Skip to content

Commit

Permalink
chore: convert xhrs js to ts (#30935)
Browse files Browse the repository at this point in the history
* Convert xhrs js to ts

* empty commit

* Clean up types and leftover todos

* install mime in net-stubbing package

* Update packages/net-stubbing/lib/server/util.ts

Co-authored-by: Bill Glesias <bglesias@gmail.com>

* Update packages/net-stubbing/lib/server/util.ts

Co-authored-by: Bill Glesias <bglesias@gmail.com>

* Remove internal function    to clean up intention of code

---------

Co-authored-by: Bill Glesias <bglesias@gmail.com>
  • Loading branch information
jennifer-shehane and AtofStryker authored Jan 28, 2025
1 parent 87a845a commit 7a72863
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 77 deletions.
32 changes: 30 additions & 2 deletions packages/net-stubbing/lib/server/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash'
import Debug from 'debug'
import mime from 'mime'
import isHtml from 'is-html'
import { IncomingMessage } from 'http'
import {
Expand All @@ -18,12 +19,39 @@ import type { CypressIncomingRequest } from '@packages/proxy'
import type { InterceptedRequest } from './intercepted-request'
import { caseInsensitiveGet, caseInsensitiveHas } from '../util'

// TODO: move this into net-stubbing once cy.route is removed
import { parseContentType } from '@packages/server/lib/controllers/xhrs'
import type { CyHttpMessages } from '../external-types'
import { getEncoding } from 'istextorbinary'

const debug = Debug('cypress:net-stubbing:server:util')
const htmlLikeRe = /<.+>[\s\S]+<\/.+>/

const isValidJSON = function (text: unknown) {
if (_.isObject(text)) {
return true
}

try {
const o = JSON.parse(text as string)

return _.isObject(o)
} catch (error) {
false
}

return false
}

export function parseContentType (response?: string) {
if (isValidJSON(response)) {
return mime.getType('json')
}

if (response && htmlLikeRe.test(response)) {
return mime.getType('html')
}

return mime.getType('text')
}

export function emit (socket: CyServer.Socket, eventName: string, data: object) {
if (debug.enabled) {
Expand Down
1 change: 1 addition & 0 deletions packages/net-stubbing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"is-html": "^2.0.0",
"istextorbinary": "6.0.0",
"lodash": "^4.17.15",
"mime": "^2.6.0",
"mime-types": "2.1.27",
"minimatch": "^3.1.2",
"throttle": "^1.0.3"
Expand Down
30 changes: 29 additions & 1 deletion packages/net-stubbing/test/unit/util-spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
import { getBodyEncoding } from '../../lib/server/util'
import { getBodyEncoding, parseContentType } from '../../lib/server/util'
import { expect } from 'chai'
import { join } from 'path'
import { readFileSync } from 'fs'

const imageBuffer = readFileSync(join(__dirname, '..', 'fixtures', 'cypress-logo.png'))

describe('net-stubbing util', () => {
describe('parseContentType', () => {
it('returns application/json', () => {
const str = JSON.stringify({ foo: 'bar' })

expect(parseContentType(str)).to.eq('application/json')
})

it('returns text/html', () => {
const str = `\
<html>
<body>foobarbaz</body>
</html>\
`

expect(parseContentType(str)).to.eq('text/html')
})

it('returns text/plain', () => {
const str = 'foobar<p>baz'

expect(parseContentType(str)).to.eq('text/plain')
})

it('returns text/plain by default', () => {
expect(parseContentType()).to.eq('text/plain')
})
})

context('getBodyEncoding', () => {
it('returns null without data', () => {
expect(getBodyEncoding(null)).to.equal(null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
const _ = require('lodash')
const mime = require('mime')
const Promise = require('bluebird')
const fixture = require('../fixture')
import { parseContentType } from '@packages/net-stubbing/lib/server/util'
import _ from 'lodash'
import Promise from 'bluebird'
import fixture from '../fixture'

const fixturesRe = /^(fx:|fixture:)/
const htmlLikeRe = /<.+>[\s\S]+<\/.+>/

const isValidJSON = function (text) {
if (_.isObject(text)) {
return true
}

try {
const o = JSON.parse(text)

return _.isObject(o)
} catch (error) {
false
}

return false
}

module.exports = {
export = {
handle (req, res, config, next) {
const get = function (val, def) {
const get = function (val, def?) {
return decodeURI(req.get(val) || def)
}

Expand All @@ -37,7 +20,7 @@ module.exports = {
// figure out the stream interface and pipe these
// chunks to the response
return this.getResponse(response, config)
.then((resp = {}) => {
.then((resp: { data: any, encoding?: BufferEncoding }) => {
let { data, encoding } = resp

// grab content-type from x-cypress-headers if present
Expand Down Expand Up @@ -73,7 +56,7 @@ module.exports = {
.set(headers)
.status(status)
.end(chunk)
}).catch((err) => {
}).catch((err: Error) => {
return res
.status(400)
.send({ __error: err.stack })
Expand All @@ -87,8 +70,8 @@ module.exports = {
return respond()
},

_get (resp, config) {
const options = {}
_get (resp: string, config: { fixturesFolder: string }): Promise<{ data: any, encoding?: string }> {
const options: { encoding?: string } = {}

const file = resp.replace(fixturesRe, '')

Expand All @@ -99,7 +82,7 @@ module.exports = {
}

return fixture.get(config.fixturesFolder, filePath, options)
.then((bytes) => {
.then((bytes: any) => {
return {
data: bytes,
encoding,
Expand All @@ -115,22 +98,6 @@ module.exports = {
return Promise.resolve({ data: resp })
},

parseContentType (response) {
const ret = (type) => {
return mime.getType(type) //+ "; charset=utf-8"
}

if (isValidJSON(response)) {
return ret('json')
}

if (htmlLikeRe.test(response)) {
return ret('html')
}

return ret('text')
},

parseHeaders (headers, response) {
try {
headers = JSON.parse(headers)
Expand All @@ -141,7 +108,7 @@ module.exports = {
}

if (headers['content-type'] == null) {
headers['content-type'] = this.parseContentType(response)
headers['content-type'] = parseContentType(response)
}

return headers
Expand Down
28 changes: 0 additions & 28 deletions packages/server/test/unit/xhrs_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,6 @@ require('../spec_helper')
const xhrs = require(`../../lib/controllers/xhrs`)

describe('lib/controllers/xhr', () => {
describe('#parseContentType', () => {
it('returns application/json', () => {
const str = JSON.stringify({ foo: 'bar' })

expect(xhrs.parseContentType(str)).to.eq('application/json')
})

it('returns text/html', () => {
const str = `\
<html>
<body>foobarbaz</body>
</html>\
`

expect(xhrs.parseContentType(str)).to.eq('text/html')
})

it('returns text/plain', () => {
const str = 'foobar<p>baz'

expect(xhrs.parseContentType(str)).to.eq('text/plain')
})

it('returns text/plain by default', () => {
expect(xhrs.parseContentType()).to.eq('text/plain')
})
})

describe('#parseHeaders', () => {
it('returns object literal on undefined', () => {
const obj = xhrs.parseHeaders(undefined)
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22392,7 +22392,7 @@ mime@1.6.0, mime@^1.3.4, mime@^1.4.1, mime@^1.6.0:
resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1"
integrity sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==

mime@2.6.0, mime@^2.4.6, mime@^2.5.2:
mime@2.6.0, mime@^2.4.6, mime@^2.5.2, mime@^2.6.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/mime/-/mime-2.6.0.tgz#a2a682a95cd4d0cb1d6257e28f83da7e35800367"
integrity sha512-USPkMeET31rOMiarsBNIHZKLGgvKc/LrjofAnBlOttf5ajRvqiRA8QsenbcooctK6d6Ts6aqZXBA+XbkKthiQg==
Expand Down

4 comments on commit 7a72863

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 7a72863 Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/14.0.1/linux-x64/develop-7a7286308847030f734d1e73458f8b6ba39e345a/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 7a72863 Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/14.0.1/linux-arm64/develop-7a7286308847030f734d1e73458f8b6ba39e345a/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 7a72863 Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/14.0.1/darwin-arm64/develop-7a7286308847030f734d1e73458f8b6ba39e345a/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 7a72863 Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/14.0.1/darwin-x64/develop-7a7286308847030f734d1e73458f8b6ba39e345a/cypress.tgz

Please sign in to comment.