Skip to content

Commit

Permalink
Revert "Use websockets to stub large XHR response bodies instead of h…
Browse files Browse the repository at this point in the history
…ea… (#5525)"

This reverts commit 249db45.
  • Loading branch information
flotwig committed Mar 11, 2020
1 parent df30ae6 commit d77c106
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 336 deletions.
6 changes: 5 additions & 1 deletion packages/driver/src/cy/commands/navigation.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ cannotVisitDifferentOrigin = (origin, previousUrlVisited, remoteUrl, existingUrl
previousUrl: previousUrlVisited
attemptedUrl: origin
}
}
}

$errUtils.throwErrByPath("visit.cannot_visit_different_origin", errOpts)

Expand Down Expand Up @@ -294,6 +294,10 @@ normalizeTimeoutOptions = (options) ->
module.exports = (Commands, Cypress, cy, state, config) ->
reset()

Cypress.on "test:before:run:async", ->
## reset any state on the backend
Cypress.backend('reset:server:state')

Cypress.on("test:before:run", reset)

Cypress.on "stability:changed", (bool, event) ->
Expand Down
9 changes: 1 addition & 8 deletions packages/driver/src/cy/commands/xhr.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ startXhrServer = (cy, state, config) ->
xhrUrl: config("xhrUrl")
stripOrigin: stripOrigin

emitIncoming: (id, route) ->
Cypress.backend('incoming:xhr', id, route)

## shouldnt these stubs be called routes?
## rename everything related to stubs => routes
onSend: (xhr, stack, route) =>
Expand Down Expand Up @@ -252,10 +249,6 @@ module.exports = (Commands, Cypress, cy, state, config) ->
## correctly
Cypress.on("window:unload", cancelPendingXhrs)

Cypress.on "test:before:run:async", ->
## reset any state on the backend
Cypress.backend('reset:server:state')

Cypress.on "test:before:run", ->
## reset the existing server
reset()
Expand All @@ -266,7 +259,7 @@ module.exports = (Commands, Cypress, cy, state, config) ->
## window such as if the last test ended
## with a cross origin window
try
server = startXhrServer(cy, state, config, Cypress)
server = startXhrServer(cy, state, config)
catch err
## in this case, just don't bind to the server
server = null
Expand Down
41 changes: 16 additions & 25 deletions packages/driver/src/cypress/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ props = "onreadystatechange onload onerror".split(" ")

restoreFn = null

setHeader = (xhr, key, val) ->
setHeader = (xhr, key, val, transformer) ->
if val?
if transformer
val = transformer(val)

key = "X-Cypress-" + _.capitalize(key)
xhr.setRequestHeader(key, encodeURI(val))

Expand Down Expand Up @@ -174,30 +177,18 @@ create = (options = {}) ->
hasEnabledStubs and route and route.response?

applyStubProperties: (xhr, route) ->
responseToString = =>
if not _.isString(route.response)
return JSON.stringify(route.response)

route.response

response = responseToString()

headers = {
"id": xhr.id
"status": route.status
"matched": route.url + ""
"delay": route.delay
"headers": transformHeaders(route.headers)
}

if response.length > 4096
options.emitIncoming(xhr.id, response)
headers.responseDeferred = true
else
headers.response = response

_.map headers, (v, k) =>
setHeader(xhr, k, v)
responser = if _.isObject(route.response) then JSON.stringify else null

## add header properties for the xhr's id
## and the testId
setHeader(xhr, "id", xhr.id)
# setHeader(xhr, "testId", options.testId)

setHeader(xhr, "status", route.status)
setHeader(xhr, "response", route.response, responser)
setHeader(xhr, "matched", route.url + "")
setHeader(xhr, "delay", route.delay)
setHeader(xhr, "headers", route.headers, transformHeaders)

route: (attrs = {}) ->
warnOnStubDeprecation(attrs, "route")
Expand Down
65 changes: 0 additions & 65 deletions packages/server/__snapshots__/4_xhr_spec.coffee.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,71 +71,6 @@ exports['e2e xhr / passes in global mode'] = `

exports['e2e xhr / passes through CLI'] = `
====================================================================================================
(Run Starting)
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Cypress: 1.2.3 │
│ Browser: FooBrowser 88 │
│ Specs: 1 found (xhr_spec.coffee) │
│ Searched: cypress/integration/xhr_spec.coffee │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
────────────────────────────────────────────────────────────────────────────────────────────────────
Running: xhr_spec.coffee (1 of 1)
xhrs
✓ can encode + decode headers
✓ ensures that request headers + body go out and reach the server unscathed
✓ does not inject into json's contents from http server even requesting text/html
✓ does not inject into json's contents from file server even requesting text/html
✓ works prior to visit
✓ can stub a 100kb response
✓ spawns tasks with original NODE_OPTIONS
server with 1 visit
✓ response body
✓ request body
✓ aborts
10 passing
(Results)
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Tests: 10 │
│ Passing: 10 │
│ Failing: 0 │
│ Pending: 0 │
│ Skipped: 0 │
│ Screenshots: 0 │
│ Video: true │
│ Duration: X seconds │
│ Spec Ran: xhr_spec.coffee │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
(Video)
- Started processing: Compressing to 32 CRF
- Finished processing: /XXX/XXX/XXX/cypress/videos/xhr_spec.coffee.mp4 (X second)
====================================================================================================
(Run Finished)
Spec Tests Passing Failing Pending Skipped
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ ✔ xhr_spec.coffee XX:XX 10 10 - - - │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
✔ All specs passed! XX:XX 10 10 - - -
`
21 changes: 4 additions & 17 deletions packages/server/lib/controllers/xhrs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ isValidJSON = (text) ->
return false

module.exports = {
handle: (req, res, getDeferredResponse, config, next) ->
handle: (req, res, config, next) ->
get = (val, def) ->
decodeURI(req.get(val) ? def)

Expand All @@ -27,10 +27,6 @@ module.exports = {
headers = get("x-cypress-headers", null)
response = get("x-cypress-response", "")

if get("x-cypress-responsedeferred", "")
id = get("x-cypress-id")
response = getDeferredResponse(id)

respond = =>
## figure out the stream interface and pipe these
## chunks to the response
Expand Down Expand Up @@ -63,10 +59,6 @@ module.exports = {
.set(headers)
.status(status)
.end(chunk)
.catch { testEndedBeforeResponseReceived: true }, ->
res
.socket
.destroy()
.catch (err) ->
res
.status(400)
Expand All @@ -92,15 +84,10 @@ module.exports = {
{data: bytes, encoding: encoding}

getResponse: (resp, config) ->
if resp.then
return resp
.then (data) =>
{ data }

if fixturesRe.test(resp)
return @_get(resp, config)

Promise.resolve({data: resp})
@_get(resp, config)
else
Promise.resolve({data: resp})

parseContentType: (response) ->
ret = (type) ->
Expand Down
4 changes: 2 additions & 2 deletions packages/server/lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const client = require('./controllers/client')
const files = require('./controllers/files')
const staticCtrl = require('./controllers/static')

module.exports = ({ app, config, getDeferredResponse, getRemoteState, networkProxy, project, onError }) => {
module.exports = ({ app, config, getRemoteState, networkProxy, project, onError }) => {
// routing for the actual specs which are processed automatically
// this could be just a regular .js file or a .coffee file
app.get('/__cypress/tests', (req, res, next) => {
Expand Down Expand Up @@ -49,7 +49,7 @@ module.exports = ({ app, config, getDeferredResponse, getRemoteState, networkPro
})

app.all('/__cypress/xhrs/*', (req, res, next) => {
xhrs.handle(req, res, getDeferredResponse, config, next)
xhrs.handle(req, res, config, next)
})

app.get('/__root/*', (req, res) => {
Expand Down
6 changes: 1 addition & 5 deletions packages/server/lib/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ logger = require("./logger")
Socket = require("./socket")
Request = require("./request")
fileServer = require("./file_server")
XhrServer = require("./xhr_ws_server")
templateEngine = require("./template_engine")

DEFAULT_DOMAIN_NAME = "localhost"
Expand Down Expand Up @@ -177,7 +176,6 @@ class Server
## TODO: might not be needed anymore
@_request = Request({timeout: config.responseTimeout})
@_nodeProxy = httpProxy.createProxyServer()
@_xhrServer = XhrServer.create()

getRemoteState = => @_getRemoteState()

Expand All @@ -190,7 +188,6 @@ class Server
@createRoutes({
app
config
getDeferredResponse: @_xhrServer.getDeferredResponse
getRemoteState
networkProxy: @_networkProxy
onError
Expand Down Expand Up @@ -742,14 +739,13 @@ class Server
startWebsockets: (automation, config, options = {}) ->
options.onResolveUrl = @_onResolveUrl.bind(@)
options.onRequest = @_onRequest.bind(@)
options.onIncomingXhr = @_xhrServer.onIncomingXhr

options.onResetServerState = =>
@_xhrServer.reset()
@_networkProxy.reset()

@_socket = new Socket(config)
@_socket.startListening(@_server, automation, config, options)
@_normalizeReqUrl(@_server)
# handleListeners(@_server)

module.exports = Server
3 changes: 0 additions & 3 deletions packages/server/lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ class Socket {

_.defaults(options, {
socketId: null,
onIncomingXhr () {},
onResetServerState () {},
onSetRunnables () {},
onMocha () {},
Expand Down Expand Up @@ -375,8 +374,6 @@ class Socket {
return firefoxUtil.log()
case 'firefox:force:gc':
return firefoxUtil.collectGarbage()
case 'incoming:xhr':
return options.onIncomingXhr(args[0], args[1])
case 'get:fixture':
return fixture.get(config.fixturesFolder, args[0], args[1])
case 'read:file':
Expand Down
93 changes: 0 additions & 93 deletions packages/server/lib/xhr_ws_server.ts

This file was deleted.

0 comments on commit d77c106

Please sign in to comment.