To send fake request to a Koa application without starting a http server.
Sending multipart data will be supported later.
$ npm i koa-sham --saveconst Koa = require( 'koa' );
const sham = require( 'koa-sham' );
const app = new Koa();
app.use( ctx => {
ctx.body = { status : 1 };
} );
sham( app, ( err, res, body ) => {
console.log( body ); // { status : 1 }
} );
sham( app, {
qs : {
x : 1
}
}, ( err, res, body ) => {
console.log( body ); // { status : 1 }
} );
sham( app, '/path', { https: true }, ( err, res, body ) => {
console.log( body ); // { status : 1 }
} );
sham( app, {
method : 'POST',
body : {
x : 1
}
}, ( err, res, body ) => {
console.log( body ); // { status : 1 }
} );sham( app, { promise : true } ).then( data => {
console.log( data );
} );sham( app ).pipe( process.stdout );Returns a Readable Stream by default, but can be changed by using { promise : true } in options.
app
The instance of Koa application.
url
Type: String
The URL or PATH the you want to request. The host of the URL will be set as 127.0.0.1 by default. The protocol will be set to http if options.https is not true. The default port is 80 and it can be changed with options.port.
If the protocol of the URL is https, the request will be set to secure, even thought the options.https is not set to true.
options
Type: Object
Options for the fake request.
-
remoteAddress
StringThe remote IP address,127.0.0.1by default. -
host
StringThe host of URL, this item will be ignored if the passedURLcontains it's host. -
port
NumberThe port of URL, this item will be ignored if the passedURLcontains it's host. -
https
BooleanTo set the request tosecure, this item will be ignored if the passedURLcontains it's protocol and is not 'https'. -
method
StringThe request method -
qs
ObjectThe query string, should be an object. -
headers
ObjectThe headers of the request. -
cookies
ObjectThe cookies that will be set while sending request, it will overwrite the same cookie which is also inheaders. -
body
ObjectStringThe request body forPOSTorPUTrequest. -
promise
BooleanIfpromiseis true, the function will return aPromiseobject. -
**resolveWithFullResponse
BooleanBy setting this option totrue, the returnedpromisewill use the full response data as it's value.