Skip to content

Commit a8b7055

Browse files
chore: remove reliance on json placeholder server in stubbing-spying__intercept example (#908)
1 parent d149522 commit a8b7055

File tree

7 files changed

+303
-17
lines changed

7 files changed

+303
-17
lines changed

examples/stubbing-spying__intercept/app.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const loadUsers = (nUsers = 3) => {
5555
console.log('loading %d users', nUsers)
5656
document.querySelector('#users').innerText = ''
5757

58-
fetch(`https://jsonplaceholder.cypress.io/users?_limit=${nUsers}`, {
58+
fetch(`http://localhost:7081/users?_limit=${nUsers}`, {
5959
method: 'GET',
6060
headers: {
6161
'Accept': 'application/json',
@@ -90,7 +90,7 @@ const loadUser = (id) => {
9090
console.log('loading user #%d', id)
9191
document.querySelector('#users').innerText = ''
9292

93-
fetch(`https://jsonplaceholder.cypress.io/users/${id}`)
93+
fetch(`http://localhost:7081/users/${id}`)
9494
.then((r) => r.json())
9595
.then((user) => {
9696
const users = [user]
@@ -116,7 +116,7 @@ function postUser () {
116116
name: 'Joe Smith',
117117
}
118118

119-
return fetch('https://jsonplaceholder.cypress.io/users', {
119+
return fetch('http://localhost:7081/users', {
120120
method: 'POST',
121121
headers: {
122122
'Accept': 'application/json',
@@ -128,11 +128,11 @@ function postUser () {
128128

129129
function putUser () {
130130
const user = {
131-
id: 101,
131+
id: 1,
132132
name: 'Joe Smith',
133133
}
134134

135-
return fetch('https://jsonplaceholder.cypress.io/users/1', {
135+
return fetch('http://localhost:7081/users/1', {
136136
method: 'PUT',
137137
headers: {
138138
'Accept': 'application/json',

examples/stubbing-spying__intercept/cypress/e2e/matching-spec.cy.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ describe('intercept', () => {
2525
// confirm that both interceptors are matched
2626
cy.wait('@users')
2727
.its('request.url')
28-
.should('equal', 'https://jsonplaceholder.cypress.io/users/2')
28+
.should('equal', 'http://localhost:7081/users/2')
2929

3030
cy.wait('@secondUser')
3131
.its('request.url')
32-
.should('equal', 'https://jsonplaceholder.cypress.io/users/2')
32+
.should('equal', 'http://localhost:7081/users/2')
3333
})
3434

3535
it('use regex to match exactly', () => {
@@ -41,7 +41,7 @@ describe('intercept', () => {
4141
// only the second interceptor should match
4242
cy.wait('@secondUser')
4343
.its('request.url')
44-
.should('equal', 'https://jsonplaceholder.cypress.io/users/2')
44+
.should('equal', 'http://localhost:7081/users/2')
4545
})
4646

4747
it('use regex to match exactly and check if the other intercept has not fired', () => {
@@ -57,7 +57,7 @@ describe('intercept', () => {
5757
// only the second interceptor should match
5858
cy.wait('@secondUser')
5959
.its('request.url')
60-
.should('equal', 'https://jsonplaceholder.cypress.io/users/2')
60+
.should('equal', 'http://localhost:7081/users/2')
6161
// but the first intercept should have never fired
6262
.then(() => {
6363
expect(usersMatched, 'users intercept').to.be.false

examples/stubbing-spying__intercept/cypress/e2e/spy-on-fetch-spec.cy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('intercept', () => {
6363
})
6464

6565
it('spying on 2nd domain', () => {
66-
cy.intercept('https://jsonplaceholder.cypress.io/users*').as('users')
66+
cy.intercept('http://localhost:7081/users*').as('users')
6767
cy.get('#load-users').click()
6868
cy.wait('@users').its('response.body').should('have.length', 3)
6969
.its('0') // grab the first user from the list

examples/stubbing-spying__intercept/cypress/e2e/stub-fetch-spec.cy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ describe('intercept', () => {
200200
username: 'Test User',
201201
}]
202202

203-
cy.intercept({ url: 'https://jsonplaceholder.cypress.io/users*' }, {
203+
cy.intercept({ url: 'http://localhost:7081/users*' }, {
204204
body: users,
205205
headers: {
206206
'access-control-allow-origin': Cypress.config('baseUrl'),

examples/stubbing-spying__intercept/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"cypress:open": "../../node_modules/.bin/cypress open",
88
"cypress:run": "../../node_modules/.bin/cypress run",
99
"cypress:run:record": "../../node_modules/.bin/cypress run --record",
10-
"dev": "../../node_modules/.bin/start-test 7080 cypress:open",
11-
"start": "node server.js --port 7080",
12-
"test:ci": "../../node_modules/.bin/start-test 7080 cypress:run",
13-
"test:ci:record": "../../node_modules/.bin/start-test 7080 cypress:run:record"
10+
"dev": "../../node_modules/.bin/start-test \"7080|7081\" cypress:open",
11+
"start": "node server.js --port1 7080 --port2 7081",
12+
"test:ci": "../../node_modules/.bin/start-test \"7080|7081\" cypress:run",
13+
"test:ci:record": "../../node_modules/.bin/start-test \"7080|7081\" cypress:run:record"
1414
}
1515
}

examples/stubbing-spying__intercept/server.js

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ const path = require('path')
44
const minimist = require('minimist')
55
const express = require('express')
66
const morgan = require('morgan')
7+
const cors = require('cors')
78

89
const fruits = require('./fruits')
10+
const users = require('./users')
911
const app = express()
1012

1113
// get port from passed in args from scripts/start.js
12-
const port = minimist(process.argv.slice(2)).port
14+
const port1 = minimist(process.argv.slice(2)).port1
15+
const port2 = minimist(process.argv.slice(2)).port2
1316

1417
app.use(morgan('dev'))
1518
app.use(express.static('.'))
@@ -76,4 +79,55 @@ app.get('/req-headers', (req, res) => {
7679
res.json(req.headers)
7780
})
7881

79-
app.listen(port)
82+
app.listen(port1)
83+
84+
const app2 = express()
85+
86+
app2.disable('etag')
87+
app2.use(morgan('dev'))
88+
app2.use(cors())
89+
app2.use(express.json())
90+
91+
app2.get('/', (req, res) => {
92+
res.sendFile(`${__dirname}/index.html`)
93+
})
94+
95+
app2.get('/users/:id', (req, res) => {
96+
const id = parseInt(req.params.id)
97+
const user = users.find((u) => u.id === id)
98+
99+
if (user) {
100+
return res.json(user)
101+
}
102+
103+
res.status(404).json({ error: 'User not found' })
104+
})
105+
106+
app2.get('/users', (req, res) => {
107+
const limit = req.query._limit
108+
109+
if (limit) {
110+
return res.json(users.slice(0, limit))
111+
}
112+
113+
return res.json(users)
114+
})
115+
116+
app2.post('/users', (req, res) => {
117+
res.sendStatus(201)
118+
})
119+
120+
app2.put('/users/:id', (req, res) => {
121+
const id = parseInt(req.params.id)
122+
const user = users.find((u) => u.id === id)
123+
124+
if (user) {
125+
return res.json({
126+
...req.body,
127+
})
128+
}
129+
130+
res.status(404).json({ error: 'User not found' })
131+
})
132+
133+
app2.listen(port2)

0 commit comments

Comments
 (0)