Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use fixture as request body shortcut #27934

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ _Released 11/21/2023 (PENDING)_
**Features:**

- When artifacts are uploaded to the Cypress Cloud, the duration of each upload will be displayed in the console. Addresses [#28237](https://github.com/cypress-io/cypress/issues/28237).
- Introduces shorthand version of using cy.request() with fixture data. Addresses [#3387](https://github.com/cypress-io/cypress/issues/3387).

**Bugfixes:**

Expand Down
4 changes: 2 additions & 2 deletions packages/driver/cypress/e2e/commands/actions/click.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3987,9 +3987,9 @@ describe('mouse state', () => {
clientX: 492,
clientY: 9,
layerX: 492,
layerY: 215,
layerY: 226,
Crustum7 marked this conversation as resolved.
Show resolved Hide resolved
pageX: 492,
pageY: 215,
pageY: 226,
screenX: 492,
screenY: 9,
x: 492,
Expand Down
16 changes: 16 additions & 0 deletions packages/driver/cypress/e2e/commands/request.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,22 @@ describe('src/cy/commands/request', () => {
cy.request()
})

it('throws when body is combined with fixture', function (done) {
cy.on('fail', (err) => {
const { lastLog } = this

assertLogLength(this.logs, 1)
expect(lastLog.get('error')).to.eq(err)
expect(lastLog.get('state')).to.eq('failed')
expect(err.message).to.eq('`cy.request()` was called with a body and a fixture which cannot be combined.')
expect(err.docsUrl).to.eq('https://on.cypress.io/request')

done()
})

cy.request({ url: 'http://localhost:8080/users', fixture: 'number', body: { data: 14 } })
})

it('throws when url is not FQDN', {
baseUrl: null,
}, function (done) {
Expand Down
11 changes: 11 additions & 0 deletions packages/driver/src/cy/commands/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const REQUEST_DEFAULTS = {
encoding: 'utf8',
gzip: true,
timeout: null,
fixture: null,
followRedirect: true,
failOnStatusCode: true,
retryIntervals: [0, 100, 200, 200],
Expand Down Expand Up @@ -138,6 +139,16 @@ export default (Commands, Cypress, cy, state, config) => {
$errUtils.throwErrByPath('request.url_wrong_type')
}

if (options.fixture) {
if (options.body) {
$errUtils.throwErrByPath('request.body_and_fixture')
}

if (!userOptions.method) {
options.method = 'POST'
}
Crustum7 marked this conversation as resolved.
Show resolved Hide resolved
}
Crustum7 marked this conversation as resolved.
Show resolved Hide resolved

// normalize the url by prepending it with our current origin
// or the baseUrl
// or just using the options.url if its FQDN
Expand Down
4 changes: 4 additions & 0 deletions packages/driver/src/cypress/error_messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,10 @@ export default {
message: `${cmd('request')} was called with an invalid method: \`{{method}}\`. Method can be: \`GET\`, \`POST\`, \`PUT\`, \`DELETE\`, \`PATCH\`, \`HEAD\`, \`OPTIONS\`, or any other method supported by Node's HTTP parser.`,
docsUrl: 'https://on.cypress.io/request',
},
body_and_fixture: {
message: `${cmd('request')} was called with a body and a fixture which cannot be combined.`,
Crustum7 marked this conversation as resolved.
Show resolved Hide resolved
docsUrl: 'https://on.cypress.io/request',
},
form_invalid: {
message: stripIndent`\
${cmd('request')} requires the \`form\` option to be a boolean.
Expand Down
6 changes: 5 additions & 1 deletion packages/server/lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,14 +662,18 @@ module.exports = function (options = {}) {
})
},

sendPromise (userAgent, automationFn, options = {}) {
async sendPromise (userAgent, automationFn, getFixture, options = {}) {
_.defaults(options, {
headers: {},
gzip: true,
cookies: true,
followRedirect: true,
})

if (options.fixture) {
options.form = await getFixture(...options.fixture.split(',', 2))
}

if (!caseInsensitiveGet(options.headers, 'user-agent') && userAgent) {
options.headers['user-agent'] = userAgent
}
Expand Down
4 changes: 2 additions & 2 deletions packages/server/lib/server-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,9 @@ export class ServerBase<TSocket extends SocketE2E | SocketCt> {
})
}

_onRequest (userAgent, automationRequest, options) {
_onRequest (userAgent, automationRequest, getFixture, options) {
// @ts-ignore
return this.request.sendPromise(userAgent, automationRequest, options)
return this.request.sendPromise(userAgent, automationRequest, getFixture, options)
}

_callRequestListeners (server, listeners, req, res) {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/lib/socket-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ export class SocketBase {
return options.onResolveUrl(url, userAgent, automationRequest, resolveOpts)
}
case 'http:request':
return options.onRequest(userAgent, automationRequest, args[0])
return options.onRequest(userAgent, automationRequest, getFixture, args[0])
case 'reset:server:state':
return options.onResetServerState()
case 'log:memory:pressure':
Expand Down
Loading