Skip to content

Commit 2947dee

Browse files
authored
Use path as alias of url (#43)
Use path as alias of url
2 parents 7b5cee2 + b78caa7 commit 2947dee

File tree

5 files changed

+66
-28
lines changed

5 files changed

+66
-28
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ File uploads (multipart/form-data) can be achieved by using [form-data](https://
4848
```js
4949
const FormData = require('form-data')
5050
const fs = require('fs')
51-
51+
5252
try {
5353
const form = new FormData()
5454
form.append('myfile', fs.createReadStream(`./path/to/file`))
5555

56-
const res = await inject(dispatch, {
57-
method: 'post',
58-
url: '/upload',
59-
payload: form,
60-
headers: form.getHeaders()
56+
const res = await inject(dispatch, {
57+
method: 'post',
58+
url: '/upload',
59+
payload: form,
60+
headers: form.getHeaders()
6161
})
6262
console.log(res.payload)
6363
} catch (err) {
@@ -111,7 +111,7 @@ Injects a fake request into an HTTP server.
111111
- `req` - a simulated request object. Inherits from `Stream.Readable`.
112112
- `res` - a simulated response object. Inherits from node's `Http.ServerResponse`.
113113
- `options` - request options object where:
114-
- `url` - a string specifying the request URL.
114+
- `url` | `path` - a string specifying the request URL.
115115
- `method` - a string specifying the HTTP request method, defaulting to `'GET'`.
116116
- `authority` - a string specifying the HTTP HOST header value to be used if no header is provided, and the `url`
117117
does not include an authority component. Defaults to `'localhost'`.

index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ declare namespace LightMyRequest {
2727
function isInjection (obj: Request | Response): boolean
2828

2929
interface InjectOptions {
30-
url: string | {
30+
url?: string | {
31+
pathname: string
32+
protocal?: string
33+
hostname?: string
34+
port?: string | number
35+
query?: string
36+
}
37+
path?: string | {
3138
pathname: string
3239
protocal?: string
3340
hostname?: string

index.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,30 @@ const Ajv = require('ajv')
66
const Request = require('./lib/request')
77
const Response = require('./lib/response')
88

9+
const urlSchema = {
10+
oneOf: [
11+
{ type: 'string' },
12+
{
13+
type: 'object',
14+
properties: {
15+
protocol: { type: 'string' },
16+
hostname: { type: 'string' },
17+
pathname: { type: 'string' }
18+
// port type => any
19+
// query type => any
20+
},
21+
additionalProperties: true,
22+
required: ['pathname']
23+
}
24+
]
25+
}
26+
927
const ajv = new Ajv()
1028
const schema = {
1129
type: 'object',
1230
properties: {
13-
url: {
14-
oneOf: [
15-
{ type: 'string' },
16-
{
17-
type: 'object',
18-
properties: {
19-
protocol: { type: 'string' },
20-
hostname: { type: 'string' },
21-
pathname: { type: 'string' }
22-
// port type => any
23-
// query type => any
24-
},
25-
additionalProperties: true,
26-
required: ['pathname']
27-
}
28-
]
29-
},
31+
url: urlSchema,
32+
path: urlSchema,
3033
headers: {
3134
type: 'object',
3235
additionalProperties: true
@@ -51,7 +54,10 @@ const schema = {
5154
// payload type => any
5255
},
5356
additionalProperties: true,
54-
required: ['url']
57+
oneOf: [
58+
{ required: ['url'] },
59+
{ required: ['path'] }
60+
]
5561
}
5662

5763
const optsValidator = ajv.compile(schema)

lib/request.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function hostHeaderFromURL (parsedURL) {
2222
*
2323
* @constructor
2424
* @param {Object} options
25-
* @param {(Object|String)} options.url
25+
* @param {(Object|String)} options.url || options.path
2626
* @param {String} [options.method='GET']
2727
* @param {String} [options.remoteAddress]
2828
* @param {Object} [options.headers]
@@ -32,7 +32,7 @@ function hostHeaderFromURL (parsedURL) {
3232
function Request (options) {
3333
Readable.call(this)
3434

35-
const parsedURL = parseURL(options.url, options.query)
35+
const parsedURL = parseURL(options.url || options.path, options.query)
3636

3737
this.url = parsedURL.pathname + parsedURL.search
3838

test/test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,31 @@ test('form-data should be handled correctly', (t) => {
884884
})
885885
})
886886

887+
test('path as alias to url', (t) => {
888+
t.plan(2)
889+
890+
const dispatch = function (req, res) {
891+
res.writeHead(200, { 'Content-Type': 'text/plain' })
892+
res.end(req.url)
893+
}
894+
895+
inject(dispatch, { method: 'GET', path: 'http://example.com:8080/hello' }, (err, res) => {
896+
t.error(err)
897+
t.strictEqual(res.payload, '/hello')
898+
})
899+
})
900+
901+
test('Should throw if both path and url are missing', (t) => {
902+
t.plan(1)
903+
904+
try {
905+
inject(() => {}, { method: 'GET' }, () => {})
906+
t.fail('Should throw')
907+
} catch (err) {
908+
t.ok(err)
909+
}
910+
})
911+
887912
function getTestStream (encoding) {
888913
const word = 'hi'
889914
let i = 0

0 commit comments

Comments
 (0)