Created
August 31, 2016 16:00
-
-
Save brian-mann/698efc1ddf092b9b2fefa2874e44c473 to your computer and use it in GitHub Desktop.
e2e test covering XHR request body + headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http = require("http") | |
morgan = require("morgan") | |
express = require("express") | |
bodyParser = require("body-parser") | |
app = express() | |
srv = http.Server(app) | |
app.use(morgan("dev")) | |
app.use(bodyParser.json()) | |
app.get "/", (req, res) -> | |
res.send("<html>hi there</html>") | |
app.post "/login", (req, res) -> | |
## respond with JSON with exactly what the | |
## request body was and all of the request headers | |
res.json({ | |
body: req.body | |
headers: req.headers | |
}) | |
srv.listen 2222, -> | |
console.log "listening on 2222" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it "ensures that request headers go out and reach the server", -> | |
cy | |
.visit("http://localhost:2222") | |
.window().then (win) -> | |
new Cypress.Promise (resolve) -> | |
xhr = new win.XMLHttpRequest | |
xhr.open("POST", "/login") | |
xhr.setRequestHeader("Content-Type", "application/json") | |
xhr.setRequestHeader("X-CSRF-Token", "abc-123") | |
xhr.send(JSON.stringify({foo: "bar"})) | |
xhr.onload = -> | |
resolve(JSON.parse(xhr.response)) | |
.then (resp) -> | |
## the server sends us back response JSON | |
## with the request details so we can verify | |
## that the backend server received exactly what we sent | |
## and the Cypress proxy did not modify this in any way | |
expect(resp.body).to.deep.eq({foo: "bar"}) | |
expect(resp.headers).to.have.property("x-csrf-token", "abc-123") | |
expect(resp.headers).to.have.property("content-type", "application/json") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment