Skip to content

Commit

Permalink
fix: Boolean and null literals should be considered valid request bod…
Browse files Browse the repository at this point in the history
…ies (#28835)

* fix(types): RequestBody type should be able to accept booleans and null values, which are all valid JSON literals

* refactor: boolean literals are valid JSON objects. Null values should also be considered valid when explicitly passed to the request function.

* refactor: body is explicitly defined when passed as positional argument or when supplied through the options object

* test: JSON literals should be parsed as valid JSON and set json=true

* docs: issue reference

* fix: boolean and null literal should be send to request promise as strings

* docs: fixes #28789 -- added issue reference

* test: tests proper conversion of JSON literals to strings.

* docs: added isssue reference

* docs: fixes #28789 -- changelog entry

* refactor: change isValidJsonObj to isValidBody

Co-authored-by: Bill Glesias <[email protected]>

* refactor: change isValidJsonObj to isValidBody

Co-authored-by: Bill Glesias <[email protected]>

* refactor: use lodash utils

Co-authored-by: Bill Glesias <[email protected]>

* Update cli/CHANGELOG.md

Co-authored-by: Bill Glesias <[email protected]>

* docs: moved entry to 13.6.5

* docs: fixed changelog entry

* Update CHANGELOG.md

---------

Co-authored-by: Bill Glesias <[email protected]>
Co-authored-by: Jennifer Shehane <[email protected]>
  • Loading branch information
3 people authored Feb 23, 2024
1 parent f3348bc commit 3e5fabc
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 4 deletions.
8 changes: 8 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<!-- See the ../guides/writing-the-cypress-changelog.md for details on writing the changelog. -->
## 13.6.7

_Released 2/27/2024 (PENDING)_

**Bugfixes:**

- Changed RequestBody type to allow for boolean and null literals to be passed as body values. [#28789](https://github.com/cypress-io/cypress/issues/28789)

## 13.6.6

_Released 2/22/2024_
Expand Down
2 changes: 1 addition & 1 deletion cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ declare namespace Cypress {
type FileContents = string | any[] | object
type HistoryDirection = 'back' | 'forward'
type HttpMethod = string
type RequestBody = string | object
type RequestBody = string | object | boolean | null
type ViewportOrientation = 'portrait' | 'landscape'
type PrevSubject = keyof PrevSubjectMap
type TestingType = 'e2e' | 'component'
Expand Down
54 changes: 54 additions & 0 deletions packages/driver/cypress/e2e/commands/request.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,60 @@ describe('src/cy/commands/request', () => {
})
})

// https://github.com/cypress-io/cypress/issues/28789
context('accepts trivial RFC 8259 compliant body objects', () => {
it('accepts body equal to true', () => {
cy.request({ method: 'POST', url: 'http://www.github.com/projects/foo', body: true }).then(function () {
this.expectOptionsToBe({
method: 'POST',
url: 'http://www.github.com/projects/foo',
body: true,
json: true,
})
})
})

it('accepts body equal to false', () => {
cy.request({ method: 'POST', url: 'http://www.github.com/projects/foo', body: false }).then(function () {
this.expectOptionsToBe({
method: 'POST',
url: 'http://www.github.com/projects/foo',
body: false,
json: true,
})
})
})

it('accepts (explicitly defined) null body', () => {
cy.request({ method: 'POST', url: 'http://www.github.com/projects/foo', body: null }).then(function () {
this.expectOptionsToBe({
method: 'POST',
url: 'http://www.github.com/projects/foo',
//body: null,
json: true,
})
})

cy.request('POST', 'http://www.github.com/projects/foo', null).then(function () {
this.expectOptionsToBe({
method: 'POST',
url: 'http://www.github.com/projects/foo',
//body: null,
json: true,
})
})

cy.request('http://www.github.com/projects/foo', null).then(function () {
this.expectOptionsToBe({
method: 'POST',
url: 'http://www.github.com/projects/foo',
//body: null,
json: true,
})
})
})
})

context('method normalization', () => {
it('uppercases method', () => {
cy.request('post', 'https://www.foo.com').then(function () {
Expand Down
11 changes: 8 additions & 3 deletions packages/driver/src/cy/commands/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ const hasFormUrlEncodedContentTypeHeader = (headers) => {
return header && (_.toLower(header) === 'content-type')
}

const isValidJsonObj = (body) => {
return _.isObject(body) && !_.isFunction(body)
const isValidBody = (body, isExplicitlyDefined: boolean = false) => {
return (_.isObject(body) || _.isBoolean(body) || (isExplicitlyDefined && _.isNull(body)))
&& !_.isFunction(body)
}

const whichAreOptional = (val, key) => {
Expand Down Expand Up @@ -81,9 +82,11 @@ export default (Commands, Cypress, cy, state, config) => {
request (...args) {
const o: any = {}
const userOptions = o
let bodyIsExplicitlyDefined = false

if (_.isObject(args[0])) {
_.extend(userOptions, args[0])
bodyIsExplicitlyDefined = _.has(args[0], 'body')
} else if (args.length === 1) {
o.url = args[0]
} else if (args.length === 2) {
Expand All @@ -96,11 +99,13 @@ export default (Commands, Cypress, cy, state, config) => {
// set url + body
o.url = args[0]
o.body = args[1]
bodyIsExplicitlyDefined = true
}
} else if (args.length === 3) {
o.method = args[0]
o.url = args[1]
o.body = args[2]
bodyIsExplicitlyDefined = true
}

let options = _.defaults({}, userOptions, REQUEST_DEFAULTS, {
Expand Down Expand Up @@ -222,7 +227,7 @@ export default (Commands, Cypress, cy, state, config) => {

// only set json to true if form isnt true
// and we have a valid object for body
if ((options.form !== true) && isValidJsonObj(options.body)) {
if ((options.form !== true) && isValidBody(options.body, bodyIsExplicitlyDefined)) {
options.json = true
}

Expand Down
5 changes: 5 additions & 0 deletions packages/server/lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,11 @@ module.exports = function (options = {}) {
// either turn these both on or off
options.followAllRedirects = options.followRedirect

// https://github.com/cypress-io/cypress/issues/28789
if (options.json === true) {
if (_.isBoolean(options.body) || _.isNull(options.body)) options.body = String(options.body)
}

if (options.form === true) {
// reset form to whatever body is
// and nuke body
Expand Down
58 changes: 58 additions & 0 deletions packages/server/test/unit/request_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,64 @@ describe('lib/request', () => {
})
})

// https://github.com/cypress-io/cypress/issues/28789
context('json=true', () => {
beforeEach(() => {
nock('http://localhost:8080')
.matchHeader('Content-Type', 'application/json')
.post('/login')
.reply(200, '<html></html>')
})

it('does not modify regular JSON objects', function () {
const init = sinon.spy(request.rp.Request.prototype, 'init')
const body = {
foo: 'bar',
}

return request.sendPromise({}, this.fn, {
url: 'http://localhost:8080/login',
method: 'POST',
cookies: false,
json: true,
body,
})
.then(() => {
expect(init).to.be.calledWithMatch({ body })
})
})

it('converts boolean JSON literals to strings', function () {
const init = sinon.spy(request.rp.Request.prototype, 'init')

return request.sendPromise({}, this.fn, {
url: 'http://localhost:8080/login',
method: 'POST',
cookies: false,
json: true,
body: true,
})
.then(() => {
expect(init).to.be.calledWithMatch({ body: 'true' })
})
})

it('converts null JSON literals to \'null\'', function () {
const init = sinon.spy(request.rp.Request.prototype, 'init')

return request.sendPromise({}, this.fn, {
url: 'http://localhost:8080/login',
method: 'POST',
cookies: false,
json: true,
body: null,
})
.then(() => {
expect(init).to.be.calledWithMatch({ body: 'null' })
})
})
})

context('bad headers', () => {
beforeEach(function (done) {
this.srv = http.createServer((req, res) => {
Expand Down

5 comments on commit 3e5fabc

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 3e5fabc Feb 23, 2024

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/13.6.7/linux-arm64/develop-3e5fabce2cc02cbb7cc78fe511965112171a939c/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 3e5fabc Feb 23, 2024

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/13.6.7/linux-x64/develop-3e5fabce2cc02cbb7cc78fe511965112171a939c/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 3e5fabc Feb 23, 2024

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/13.6.7/darwin-x64/develop-3e5fabce2cc02cbb7cc78fe511965112171a939c/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 3e5fabc Feb 23, 2024

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/13.6.7/darwin-arm64/develop-3e5fabce2cc02cbb7cc78fe511965112171a939c/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 3e5fabc Feb 23, 2024

Choose a reason for hiding this comment

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

Circle has built the win32 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/13.6.7/win32-x64/develop-3e5fabce2cc02cbb7cc78fe511965112171a939c/cypress.tgz

Please sign in to comment.