{"_id":"@astrocoders/create-test-server","_rev":"4-70e16b82667708c5adc385d1e4be78b0","name":"@astrocoders/create-test-server","dist-tags":{"latest":"3.0.2"},"versions":{"3.0.2":{"name":"@astrocoders/create-test-server","version":"3.0.2","description":"Creates a minimal Express server for testing","main":"src/index.js","scripts":{"test":"nyc ava","coverage":"nyc report --reporter=text-lcov | coveralls"},"repository":{"type":"git","url":"git+https://github.com/Astrocoders/create-test-server.git"},"keywords":["create","test","server","mock","nock","testing","ci","unit","tests","ssl","https","http","express"],"author":{"name":"Astrocoders","email":"george.lima@astrocoders.com"},"license":"MIT","bugs":{"url":"https://github.com/Astrocoders/create-test-server/issues"},"homepage":"https://github.com/Astrocoders/create-test-server","dependencies":{"body-parser":"^1.18.2","create-cert":"^1.0.2","express":"^4.15.3","pify":"^3.0.0"},"devDependencies":{"ava":"^0.25.0","coveralls":"^3.0.0","eslint-config-xo-lukechilds":"^1.0.0","got":"^8.0.0","nyc":"^11.0.2"},"gitHead":"2357c501af576e0f12226f105b39de5f61b2c21a","_id":"@astrocoders/create-test-server@3.0.2","_nodeVersion":"11.14.0","_npmVersion":"6.7.0","dist":{"integrity":"sha512-I11U6GJzF2U3akSOrE5uR+Woqfm33x9U4iiT1N71RXcb3UyaYzXH5eD2LcLCCcMCtwlMyFdJBt2WWnGjkvd/aw==","shasum":"d849279d0e63e2c293e32c3db70eac4799cfeb9d","tarball":"https://registry.npmjs.org/@astrocoders/create-test-server/-/create-test-server-3.0.2.tgz","fileCount":6,"unpackedSize":18642,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJc7EGHCRA9TVsSAnZWagAAvs0P/jsG6+i+BduGmpVnRgAH\n8pD/NDbkmeSTxED8H0P3TAvEKFZsgw+ogYO2fVs3MzhYziGzfmet7y7Np2ba\nAMAAqb7Oz3/y70etC9BcvyBACuAdhY1vz9fo3vuMVsoFJoQ7qEe5phEjlGuo\nyOKqarP1JGQZkXmkC8g/EopWy8fcFG8oexzAWihXXdv6ElNmvajvPsUakZEg\neZEpYTiqnYOKh74PbQ++7bnU88tuIlXHmhJi85nPyXDPhEs6SRcmZJuNTgR7\nEV7BZhQBC2x8HhltsAJ1bWtGlxo7Ps9PGB9p+nu62WLAQJfoznoPtP6LB3E8\njKhAIEw0EC/oQGYyGUHS1UQxxSuEgSpeK0Nr5AoexSsuYmR9XTssdTn+07pC\nr7WRvZqyb5xQXrQWnjmB++nacqGNc5kDKnGQ4QX2V7OFHP9W8k7gFgXADS6i\n/bAdouMzfbOAvd0OVb9a1WzvqXIZx3W9aaUEQTDj21hx5o0flWt6bj5XJdNN\nAothZTPNPj78zj6M1BV7pQYgPTIQq6Keu5TetfG8gjWex5koelEqyfkHYl4M\nJ9QAzJ4XGDsfYI0AyP17HgcmxrbHUb/U7CXpQ2mbwd13WpHlZ7wzR+d3ogof\nYllTgfCaGFSI7fkk3VMDrAHlJ263WGGRq1bclkTilcO3goKHNkA2KzDTPEoq\nSUF5\r\n=HVHx\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIB1TebOjKfXLTncORoQCoQRSQgkLXkwnNcQbyon/h6v5AiEA1/0LdsRl5jOpYP/k+KoPOFj+Y7vCFJHwLS+nChKtJo4="}]},"maintainers":[{"name":"eliabejr","email":"eliabesajunior@gmail.com"},{"name":"medson10","email":"medsonoliveira@outlook.com.br"},{"name":"gabrielrubens","email":"grsabreu@gmail.com"},{"name":"georgelima","email":"georgelima11@hotmail.com"},{"name":"guilhermedecampo","email":"guilherme.decampo@astrocoders.com"}],"_npmUser":{"name":"georgelima","email":"georgelima11@hotmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/create-test-server_3.0.2_1558987142616_0.04757199515408028"},"_hasShrinkwrap":false}},"time":{"created":"2019-05-27T19:59:02.470Z","3.0.2":"2019-05-27T19:59:02.735Z","modified":"2022-04-04T15:54:35.426Z"},"maintainers":[{"email":"gameplayteam12@gmail.com","name":"luizmoratelli"},{"email":"yuri.fabris@gmail.com","name":"yurijean"},{"email":"lucassilvabesen@gmail.com","name":"lucasbesen"},{"email":"eliabesajunior@gmail.com","name":"eliabejr"},{"email":"medsonoliveira@outlook.com.br","name":"medson10"},{"email":"grsabreu@gmail.com","name":"gabrielrubens"},{"email":"georgelima11@hotmail.com","name":"georgelima"},{"email":"guilherme.decampo@astrocoders.com","name":"guilhermedecampo"}],"description":"Creates a minimal Express server for testing","homepage":"https://github.com/Astrocoders/create-test-server","keywords":["create","test","server","mock","nock","testing","ci","unit","tests","ssl","https","http","express"],"repository":{"type":"git","url":"git+https://github.com/Astrocoders/create-test-server.git"},"author":{"name":"Astrocoders","email":"george.lima@astrocoders.com"},"bugs":{"url":"https://github.com/Astrocoders/create-test-server/issues"},"license":"MIT","readme":"# create-test-server\n\n> Creates a minimal Express server for testing\n\n[![Build Status](https://travis-ci.org/lukechilds/create-test-server.svg?branch=master)](https://travis-ci.org/lukechilds/create-test-server)\n[![Coverage Status](https://coveralls.io/repos/github/lukechilds/create-test-server/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/create-test-server?branch=master)\n[![npm](https://img.shields.io/npm/dm/create-test-server.svg)](https://www.npmjs.com/package/create-test-server)\n[![npm](https://img.shields.io/npm/v/create-test-server.svg)](https://www.npmjs.com/package/create-test-server)\n\nInspired by the `createServer()` helper function in the [Got tests](https://github.com/sindresorhus/got/blob/1f1b6ffb6da13f483ef7f6bd92dd33f022e7de47/test/helpers/server.js).\n\nA simple interface for creating a preconfigured Express instance listening for both HTTP and HTTPS traffic.\n\nPorts are chosen at random for HTTP/HTTPS. A self signed certificate is automatically generated, along with an associated CA certificate for you to validate against.\n\nCreated because mocking is dirty and can [break between Node.js releases](https://github.com/node-nock/nock/issues/922). Why mock HTTP requests when you can test locally against a real server in a few lines code?\n\n## Install\n\n```shell\nnpm install --save-dev create-test-server\n```\n\n## Usage\n\n```js\nconst createTestServer = require('create-test-server');\n\nconst server = await createTestServer();\nconsole.log(server.url);\n// http://localhost:5486\nconsole.log(server.sslUrl);\n// https://localhost:5487\n\n// This is just an Express route\n// You could use any Express middleware too\nserver.get('/foo', (req, res) => {\n  res.send('bar');\n});\n\n// You can return a body directly too\nserver.get('/foo', () => 'bar');\nserver.get('/foo', 'bar');\n\n// server.url + '/foo' and server.sslUrl + '/foo' will respond with 'bar'\n```\n\nThe following `Content-Type` headers will be parsed and exposed via `req.body`:\n\n- JSON (`application/json`)\n- Text (`text/plain`)\n- URL-encoded form (`application/x-www-form-urlencoded`)\n- Buffer (`application/octet-stream`)\n\nYou can change body parsing behaviour with the [`bodyParser`](#optionsbodyparser) option.\n\n`createTestServer()` has a Promise based API that pairs well with a modern asynchronous test runner such as [AVA](https://github.com/avajs/ava).\n\nYou can create a separate server per test:\n\n```js\nimport test from 'ava';\nimport got from 'got';\nimport createTestServer from 'create-test-server';\n\ntest(async t => {\n  const server = await createTestServer();\n  server.get('/foo', 'bar');\n\n  const response = await got(`${server.url}/foo`);\n  t.is(response.body, 'bar');\n\n  await server.close();\n});\n```\n\nOr share a server across multiple tests:\n\n```js\nlet server;\n\ntest.before(async () => {\n  server = await createTestServer();\n  server.get('/foo', 'bar');\n});\n\ntest(async t => {\n  const response = await got(`${server.url}/foo`);\n  t.is(response.body, 'bar');\n});\n\ntest(async t => {\n  const response = await got(`${server.url}/foo`);\n  t.is(response.statusCode, 200);\n});\n\ntest.after(async () => {\n\tawait server.close();\n});\n```\n\nYou can also make properly authenticated SSL requests by setting a common name for the server certificate and validating against the provided CA certificate:\n\n```js\ntest(async t => {\n  const server = await createTestServer({ certificate: 'foobar.com' });\n  server.get('/foo', 'bar');\n\n  const response = await got(`${server.sslUrl}/foo`, {\n    ca: server.caCert,\n    headers: { host: 'foobar.com' }\n  });\n  t.is(response.body, 'bar');\n\n  await server.close();\n});\n```\n\nYou can still make an SSL connection without messing about with certificates if your client supports unauthorised SSL requests:\n\n```js\ntest(async t => {\n  const server = await createTestServer();\n  server.get('/foo', 'bar');\n\n  const response = await got(`${server.sslUrl}/foo`, {\n    rejectUnauthorized: false\n  });\n  t.is(response.body, 'bar');\n\n  await server.close();\n});\n```\n\nYou can also easily stop/restart the server. Notice how a new port is used when we listen again:\n\n```js\nconst server = await createTestServer();\nconsole.log(server.port);\n// 56711\n\nawait server.close();\nconsole.log(server.port);\n// undefined\n\nawait server.listen();\nconsole.log(server.port);\n// 56804\n```\n\n## API\n\n### createTestServer([options])\n\nReturns a Promise which resolves to an (already listening) server.\n\n#### options\n\nType: `object`\n\n##### options.certificate\n\nType: `string`, `object`<br>\nDefault: `undefined`\n\nSSL certificate options to be passed to [`createCert()`](https://github.com/lukechilds/create-cert).\n\n##### options.bodyParser\n\nType: `object | boolean`<br>\nDefault: `undefined`\n\nBody parser options object to be passed to [`body-parser`](https://github.com/expressjs/body-parser) methods.\n\nIf set to `false` then all body parsing middleware will be disabled.\n\n### server\n\nExpress instance resolved from `createTestServer()`\n\nThis is just a normal Express instance with a few extra properties.\n\n#### server.url\n\nType: `string`, `undefined`\n\nThe url you can reach the HTTP server on.\n\ne.g: `'http://localhost:5486'`\n\n`undefined` while the server is not listening.\n\n#### server.port\n\nType: `number`, `undefined`\n\nThe port number you can reach the HTTP server on.\n\ne.g: `5486`\n\n`undefined` while the server is not listening.\n\n#### server.sslUrl\n\nType: `string`, `undefined`\n\nThe url you can reach the HTTPS server on.\n\ne.g: `'https://localhost:5487'`\n\n`undefined` while the server is not listening.\n\n#### server.sslPort\n\nType: `number`, `undefined`\n\nThe port number you can reach the HTTPS server on.\n\ne.g: `5487`\n\n`undefined` while the server is not listening.\n\n#### server.caCert\n\nType: `string`\n\nThe CA certificate to validate the server certificate against.\n\n#### server.http\n\nType: [`http.server`](https://nodejs.org/api/http.html#http_class_http_server)\n\nThe underlying HTTP server instance.\n\n#### server.https\n\nType: [`https.server`](https://nodejs.org/api/https.html#https_class_https_server)\n\nThe underlying HTTPS server instance.\n\n#### server.listen()\n\nType: `function`\n\nReturns a Promise that resolves when both the HTTP and HTTPS servers are listening.\n\nOnce the servers are listening, `server.url` and `server.sslUrl` will be updated.\n\nPlease note, this function doesn't take a port argument, it uses a new randomised port each time. Also, you don't need to manually call this after creating a server, it will start listening automatically.\n\n#### server.close()\n\nType: `function`\n\nReturns a Promise that resolves when both the HTTP and HTTPS servers have stopped listening.\n\nOnce the servers have stopped listening, `server.url` and `server.sslUrl` will be set to `undefined`.\n\n## Related\n\n- [`create-cert`](https://github.com/lukechilds/create-cert) - Super simple self signed certificates\n\n## License\n\nMIT © Luke Childs\n","readmeFilename":"README.md"}