Skip to content

Commit

Permalink
test: refactor requests with node-fetch
Browse files Browse the repository at this point in the history
request-promise-native is deprecated
  • Loading branch information
NicoPennec committed Jul 23, 2020
1 parent b95ec33 commit 44a2e73
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 57 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@
"husky": "latest",
"jest": "latest",
"lint-staged": "latest",
"node-fetch": "latest",
"nuxt": "latest",
"nuxt-i18n": "latest",
"prettier": "latest",
"request-promise-native": "latest",
"standard-version": "latest"
},
"engines": {
Expand Down
98 changes: 44 additions & 54 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ const { readFileSync } = require('fs')
const { resolve } = require('path')
const { gunzipSync } = require('zlib')

const fetch = require('node-fetch')
const { Nuxt, Builder, Generator } = require('nuxt')
const request = require('request-promise-native')

const config = require('./fixture/nuxt.config')
config.dev = false
config.modules = [require('..')]
config.sitemap = {}

const url = (path) => `http://localhost:3000${path}`
const get = (path, options = null) => request(url(path), options)
const getGzip = (path) => request({ url: url(path), encoding: null })
const PORT = 3000
const url = (path) => `http://localhost:${PORT}${path}`
const request = (path, options = {}) => fetch(url(path), options)
const requestGzip = (path, options = {}) => request(path, { compress: true, ...options })
const get = (path) => request(path).then((res) => res.text())
const getGzip = (path) => request(path, { compress: true }).then((res) => res.buffer())

const startServer = async (config) => {
const nuxt = new Nuxt(config)
await nuxt.ready()
await new Builder(nuxt).build()
await nuxt.listen(3000)
await nuxt.listen(PORT)
return nuxt
}
const runGenerate = async (config) => {
Expand Down Expand Up @@ -250,36 +253,31 @@ describe('sitemap - advanced configuration', () => {
},
})

const requestOptions = {
simple: false,
resolveWithFullResponse: true,
}

// 1st call
let response = await get('/sitemap.xml', requestOptions)
expect(response.statusCode).toEqual(200)
expect(response.headers.etag).not.toBeUndefined()
let response = await request('/sitemap.xml')
let etag = response.headers.get('etag')
expect(response.status).toEqual(200)
expect(etag).toBeTruthy()
// 2nd call
response = await get('/sitemap.xml', {
response = await request('/sitemap.xml', {
headers: {
'If-None-Match': response.headers.etag,
'If-None-Match': etag,
},
...requestOptions,
})
expect(response.statusCode).toEqual(304)
expect(response.status).toEqual(304)

// 1st call
response = await get('/sitemap.xml.gz', requestOptions)
expect(response.statusCode).toEqual(200)
expect(response.headers.etag).not.toBeUndefined()
response = await requestGzip('/sitemap.xml.gz')
etag = response.headers.get('etag')
expect(response.status).toEqual(200)
expect(etag).toBeTruthy()
// 2nd call
response = await get('/sitemap.xml.gz', {
response = await requestGzip('/sitemap.xml.gz', {
headers: {
'If-None-Match': response.headers.etag,
'If-None-Match': etag,
},
...requestOptions,
})
expect(response.statusCode).toEqual(304)
expect(response.status).toEqual(304)
})

test('etag disabled', async () => {
Expand All @@ -291,18 +289,15 @@ describe('sitemap - advanced configuration', () => {
},
})

const requestOptions = {
simple: false,
resolveWithFullResponse: true,
}

let response = await get('/sitemap.xml', requestOptions)
expect(response.statusCode).toEqual(200)
expect(response.headers.etag).toBeUndefined()
let response = await request('/sitemap.xml')
let etag = response.headers.get('etag')
expect(response.status).toEqual(200)
expect(etag).not.toBeTruthy()

response = await get('/sitemap.xml.gz', requestOptions)
expect(response.statusCode).toEqual(200)
expect(response.headers.etag).toBeUndefined()
response = await requestGzip('/sitemap.xml.gz')
etag = response.headers.get('etag')
expect(response.status).toEqual(200)
expect(etag).not.toBeTruthy()
})

test('gzip enabled', async () => {
Expand Down Expand Up @@ -648,36 +643,31 @@ describe('sitemapindex - advanced configuration', () => {
})

test('etag enabled', async () => {
const requestOptions = {
simple: false,
resolveWithFullResponse: true,
}

// 1st call
let response = await get('/sitemapindex.xml', requestOptions)
expect(response.statusCode).toEqual(200)
expect(response.headers.etag).not.toBeUndefined()
let response = await request('/sitemapindex.xml')
let etag = response.headers.get('etag')
expect(response.status).toEqual(200)
expect(etag).toBeTruthy()
// 2nd call
response = await get('/sitemapindex.xml', {
response = await request('/sitemapindex.xml', {
headers: {
'If-None-Match': response.headers.etag,
'If-None-Match': etag,
},
...requestOptions,
})
expect(response.statusCode).toEqual(304)
expect(response.status).toEqual(304)

// 1st call
response = await get('/sitemapindex.xml.gz', requestOptions)
expect(response.statusCode).toEqual(200)
expect(response.headers.etag).not.toBeUndefined()
response = await requestGzip('/sitemapindex.xml.gz')
etag = response.headers.get('etag')
expect(response.status).toEqual(200)
expect(etag).toBeTruthy()
// 2nd call
response = await get('/sitemapindex.xml.gz', {
response = await requestGzip('/sitemapindex.xml.gz', {
headers: {
'If-None-Match': response.headers.etag,
'If-None-Match': etag,
},
...requestOptions,
})
expect(response.statusCode).toEqual(304)
expect(response.status).toEqual(304)
})

test('gzip enabled', async () => {
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7641,7 +7641,7 @@ no-case@^3.0.3:
lower-case "^2.0.1"
tslib "^1.10.0"

node-fetch@^2.2.0, node-fetch@^2.6.0:
node-fetch@^2.2.0, node-fetch@^2.6.0, node-fetch@latest:
version "2.6.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"
integrity sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==
Expand Down Expand Up @@ -9544,7 +9544,7 @@ request-promise-core@1.1.3:
dependencies:
lodash "^4.17.15"

request-promise-native@^1.0.8, request-promise-native@latest:
request-promise-native@^1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/request-promise-native/-/request-promise-native-1.0.8.tgz#a455b960b826e44e2bf8999af64dff2bfe58cb36"
integrity sha512-dapwLGqkHtwL5AEbfenuzjTYg35Jd6KPytsC2/TLkVMz8rm+tNt72MGUWT1RP/aYawMpN6HqbNGBQaRcBtjQMQ==
Expand Down

0 comments on commit 44a2e73

Please sign in to comment.