Skip to content

Instantly share code, notes, and snippets.

@brian-mann
Created August 31, 2016 16:00
Show Gist options
  • Save brian-mann/698efc1ddf092b9b2fefa2874e44c473 to your computer and use it in GitHub Desktop.
Save brian-mann/698efc1ddf092b9b2fefa2874e44c473 to your computer and use it in GitHub Desktop.
e2e test covering XHR request body + headers
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"
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