Sage
Greetings, I’m Sage - a chainable HTTP Testing Assistant. Not meant to be used directly.
Type parameters
• T
Constructors
new Sage(sageServer, method, path, config)
new Sage<
T
>(sageServer
,method
,path
,config
):Sage
<T
>
Sets the HTTP method and path for the request. Not meant to be called directly.
Parameters
• sageServer: SageServer
• method: HttpMethod
• path: string
• config: SageConfig
Returns
Sage
<T
>
Source
Methods
attach()
attach(
field
,file
,options
?):this
Method is designed to work only with FormData requests. Cannot be combined with .send(). If file is a relative path, it will be treated starting from the working directory (process.cwd()). When using form-data requests, you can skip setting the Content-Type header. It will be set automatically.
Parameters
• field: string
• file: string
| Readable
| Buffer
| Blob
a Blob, Buffer, Readable stream or a file path either staring from the working directory, or an absolute path
• options?: string
| FormDataOptions
you can pass either object with type and filename or just a string with filename
Returns
this
Throws
SageException if body is already set
Source
auth()
auth(
usernameOrToken
,password
?):this
If password is provided, Basic Auth header will be added. If password is not provided, Bearer token header will be added. Automatically adds Basic or Bearer prefix to the token.
Parameters
• usernameOrToken: string
• password?: string
Returns
this
Source
cookie()
cookie(
key
,value
):this
Parameters
• key: string
• value: string
Returns
this
Source
expect()
expect(header, expectedHeader)
expect(
header
,expectedHeader
):this
Parameters
• header: string
• expectedHeader: string
| RegExp
| string
[]
Returns
this
Source
expect(statuses)
expect(
statuses
):this
Parameters
• statuses: (100
| 101
| 102
| 103
| 200
| 201
| 202
| 203
| 204
| 205
| 206
| 207
| 300
| 301
| 302
| 303
| 304
| 305
| 307
| 308
| 400
| 401
| 402
| 403
| 404
| 405
| 406
| 407
| 408
| 409
| 410
| 411
| 412
| 413
| 414
| 415
| 416
| 417
| 418
| 419
| 420
| 421
| 422
| 423
| 424
| 426
| 428
| 429
| 431
| 451
| 500
| 501
| 502
| 503
| 504
| 505
| 507
| 511
)[]
Returns
this
Source
expect(status)
expect(
status
):this
Parameters
• status: 100
| 101
| 102
| 103
| 200
| 201
| 202
| 203
| 204
| 205
| 206
| 207
| 300
| 301
| 302
| 303
| 304
| 305
| 307
| 308
| 400
| 401
| 402
| 403
| 404
| 405
| 406
| 407
| 408
| 409
| 410
| 411
| 412
| 413
| 414
| 415
| 416
| 417
| 418
| 419
| 420
| 421
| 422
| 423
| 424
| 426
| 428
| 429
| 431
| 451
| 500
| 501
| 502
| 503
| 504
| 505
| 507
| 511
Returns
this
Source
field()
field(
field
,value
):this
When using form-data requests, you can skip setting the Content-Type header. It will be set automatically.
Parameters
• field: string
• value: string
| number
| boolean
| string
[]
Returns
this
Source
query()
query(
query
):this
Sets query parameters for the request. Also, the URI spec is quite vague about query params, and their handling is different from environment to environment. I’d advise encoding complex query params into base64 and passing them as a single string.
Parameters
• query: object
Supposed to be just a record of strings and numbers. Other values/types will be ignored.
Returns
this
Source
send()
send(
body
?):this
Sets body payload for the request. If body is an object, it will be stringified to JSON. Content-Type will be set to application/json. If body is a string, it will be used as is. Content-Type will remain plain/text. If body is a Readable stream, it will be used as is. If you need to set a different Content-Type, use the set method.
Parameters
• body?: string
| object
Returns
this
Throws
SageException if formData is already set
Source
set()
set(key)
set(
key
):this
Sets a header for the request. Consider using this at the end of the chain if you want to override any of the defaults. Note: you can pass multiple values as an array for a request header. However, response headers are always concatenated into a string with a comma separator. It’s done for the sake of testing simplicity.
Parameters
• key: IncomingHttpHeaders
Returns
this
Source
set(key, value)
set(
key
,value
):this
Parameters
• key: string
• value: string
| string
[]
Returns
this
Source
set(key, value)
set(
key
,value
):this
Parameters
• key: string
• value: string
Returns
this
Source
then()
then(
resolve
,reject
):Promise
<void
>
Parameters
• resolve: ThenableResolve
<SageHttpResponse
<T
>>
• reject: ThenableReject
Returns
Promise
<void
>
Source
fromRequestLine()
static
fromRequestLine<T
>(sageServer
,method
,path
,config
):Sage
<T
>
Request Line term is taken from HTTP spec. https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
Type parameters
• T
Parameters
• sageServer: SageServer
• method: HttpMethod
• path: string
• config: SageConfig
Returns
Sage
<T
>