|
63 | 63 | <div class="banner"> |
64 | 64 | <div class="container"> |
65 | 65 | <h1>Network Requests</h1> |
66 | | - <p>Examples of handling AJAX or XHR requests in Cypress, for a full reference of commands, go to <a href="https://on.cypress.io/api" target="_blank">docs.cypress.io</a> |
| 66 | + <p>Examples of handling HTTP requests in Cypress, for a full reference of commands, go to <a href="https://on.cypress.io/api" target="_blank">docs.cypress.io</a> |
67 | 67 | </p> |
68 | 68 | </div> |
69 | 69 | </div> |
70 | 70 | <div class="container content-container"> |
71 | 71 | <div id="network-requests"> |
72 | 72 | <div class="row"> |
73 | | - |
74 | | - <div class="col-xs-7"> |
75 | | - <h4 id="server"><a href="https://on.cypress.io/server">cy.server()</a></h4> |
76 | | - <p>To control the behavior of network requests and responses, use the <a href="https://on.cypress.io/server"><code>cy.server()</code></a> command.</p> |
77 | | - <pre><code class="javascript">cy.server().should((server) => { |
78 | | - // the default options on server |
79 | | - // you can override any of these options |
80 | | - expect(server.delay).to.eq(0) |
81 | | - expect(server.method).to.eq('GET') |
82 | | - expect(server.status).to.eq(200) |
83 | | - expect(server.headers).to.be.null |
84 | | - expect(server.response).to.be.null |
85 | | - expect(server.onRequest).to.be.undefined |
86 | | - expect(server.onResponse).to.be.undefined |
87 | | - expect(server.onAbort).to.be.undefined |
88 | | - |
89 | | - // These options control the server behavior |
90 | | - // affecting all requests |
91 | | - |
92 | | - // pass false to disable existing route stubs |
93 | | - expect(server.enable).to.be.true |
94 | | - // forces requests that don't match your routes to 404 |
95 | | - expect(server.force404).to.be.false |
96 | | - // ignores requests from ever being logged or stubbed |
97 | | - expect(server.ignore).to.be.a('function') |
98 | | -}) |
99 | | - |
100 | | -cy.server({ |
101 | | - method: 'POST', |
102 | | - delay: 1000, |
103 | | - status: 422, |
104 | | - response: {}, |
105 | | -})</code></pre> |
106 | | - </div> |
107 | | - <div class="col-xs-5"> |
108 | | - </div> |
109 | | - |
110 | | - <div class="col-xs-12"><hr></div> |
111 | | - |
112 | 73 | <div class="col-xs-7"> |
113 | 74 | <h4 id="request"><a href="https://on.cypress.io/request">cy.request()</a></h4> |
114 | 75 | <p>To make an XHR request, use the <a href="https://on.cypress.io/request"><code>cy.request()</code></a> command.</p> |
@@ -181,43 +142,41 @@ <h4 id="request"><a href="https://on.cypress.io/request">cy.request()</a></h4> |
181 | 142 | <div class="col-xs-12"><hr></div> |
182 | 143 |
|
183 | 144 | <div class="col-xs-7"> |
184 | | - <h4 id="route"><a href="https://on.cypress.io/route">cy.route()</a></h4> |
185 | | - <p>To route responses to matching requests, use the <a href="https://on.cypress.io/route"><code>cy.route()</code></a> command.</p> |
186 | | - <pre><code class="javascript">let message = 'whoa, this comment does not exist' |
187 | | -cy.server() |
| 145 | + <h4 id="http"><a href="https://on.cypress.io/http">cy.intercept()</a></h4> |
| 146 | + <p>To route responses to matching requests, use the <a href="https://on.cypress.io/http"><code>cy.intercept()</code></a> command.</p> |
| 147 | + <pre><code class="javascript"> let message = 'whoa, this comment does not exist' |
188 | 148 |
|
189 | 149 | // Listen to GET to comments/1 |
190 | | -cy.route('GET', 'comments/*').as('getComment') |
| 150 | +cy.intercept('GET', '**/comments/*').as('getComment') |
191 | 151 |
|
192 | 152 | // we have code that gets a comment when |
193 | 153 | // the button is clicked in scripts.js |
194 | 154 | cy.get('.network-btn').click() |
195 | 155 |
|
196 | 156 | // https://on.cypress.io/wait |
197 | | -cy.wait('@getComment').its('status').should('eq', 200) |
| 157 | +cy.wait('@getComment').its('response.statusCode').should('be.oneOf', [200, 304]) |
198 | 158 |
|
199 | 159 | // Listen to POST to comments |
200 | | -cy.route('POST', '/comments').as('postComment') |
| 160 | +cy.intercept('POST', '**/comments').as('postComment') |
201 | 161 |
|
202 | 162 | // we have code that posts a comment when |
203 | 163 | // the button is clicked in scripts.js |
204 | 164 | cy.get('.network-post').click() |
205 | | -cy.wait('@postComment') |
206 | | - |
207 | | -// get the route |
208 | | -cy.get('@postComment').should((xhr) => { |
209 | | - expect(xhr.requestBody).to.include('email') |
210 | | - expect(xhr.requestHeaders).to.have.property('Content-Type') |
211 | | - expect(xhr.responseBody).to.have.property('name', 'Using POST in cy.route()') |
| 165 | +cy.wait('@postComment').should(({ request, response }) => { |
| 166 | + expect(request.body).to.include('email') |
| 167 | + expect(request.headers).to.have.property('content-type') |
| 168 | + expect(response && response.body).to.have.property('name', 'Using POST in cy.intercept()') |
212 | 169 | }) |
213 | 170 |
|
214 | 171 | // Stub a response to PUT comments/ **** |
215 | | -cy.route({ |
| 172 | +cy.intercept({ |
216 | 173 | method: 'PUT', |
217 | | - url: 'comments/*', |
218 | | - status: 404, |
219 | | - response: { error: message }, |
220 | | - delay: 500, |
| 174 | + url: '**/comments/*', |
| 175 | +}, { |
| 176 | + statusCode: 404, |
| 177 | + body: { error: message }, |
| 178 | + headers: { 'access-control-allow-origin': '*' }, |
| 179 | + delayMs: 500, |
221 | 180 | }).as('putComment') |
222 | 181 |
|
223 | 182 | // we have code that puts a comment when |
|
0 commit comments