A k6 extension enables mocking HTTP(S) servers during test development.
The design of the library was inspired by Express.js. If you have already known Express.js framework, using this library should be very simple.
import http, { mock } from 'k6/x/mock'
mock('https://example.com', app => {
app.get('/', (req, res) => {
res.json({ greeting: 'Hello World!' })
})
})
export default async function () {
const res = await http.asyncRequest('GET', 'https://example.com')
console.log(res.json())
}
- Starts mock HTTP server(s) inside the k6 process
- Familiar, Express like mock route definitions
- Almost transparent for test scripts: just change import statement from
k6/http
tok6/x/mock
- Helps testing k6 tests with mock server
- Supports sync and async
k6/http
API
Note The implementation of a micro web framework (similar to Express.js) was moved to muxpress project. (Just in case you're interested in goja)
You can download pre-built k6 binaries from Releases page. Check Packages page for pre-built k6 Docker images.
You can build the k6 binary on various platforms, each with its requirements. The following shows how to build k6 binary with this extension on GNU/Linux distributions.
You must have the latest Go version installed to build the k6 binary. The latest version should match k6 and xk6.
-
Install
xk6
:go install go.k6.io/xk6/cmd/xk6@latest
-
Build the binary:
xk6 build --with github.com/szkiba/xk6-mock@latest
Note You can always use the latest version of k6 to build the extension, but the earliest version of k6 that supports extensions via xk6 is v0.43.0. The xk6 is constantly evolving, so some APIs may not be backward compatible.
If you want to add a feature or make a fix, clone the project and build it using the following commands. The xk6 will force the build to use the local clone instead of fetching the latest version from the repository. This process enables you to update the code and test it locally.
git clone git@github.com:szkiba/xk6-mock.git && cd xk6-mock
xk6 build --with github.com/szkiba/xk6-mock@latest=.
You can also use pre-built k6 image within a Docker container. In order to do that, you will need to execute something like the following:
Linux
docker run -v $(pwd):/scripts -it --rm ghcr.io/szkiba/xk6-mock:latest run --out=dashboard /scripts/script.js
Windows
docker run -v %cd%:/scripts -it --rm ghcr.io/szkiba/xk6-mock:latest run --out=dashboard /scripts/script.js
There are many examples in the scripts directory that show how to use various features of the extension.
Mixing synchronous and asynchronous calls can block k6 test execution. Both synchronous (http.get
, http.post
, ...) and asynchronous (http.asyncRequest
) API mocking is supported. By default the new asynchronous mode will be used.
To switch to synchronous mode you can pass an options object to mock
function with sync
property set to true
:
mock({ sync:true }, "https://example.com", app => {
})
The Application
class constructor also accepts similar options parameter:
const app = new Application({ sync: true })
The k6/x/mock
module contains a thin wrapper around the k6/http
module. This allow to use k6/x/mock
module as drop-in replacement for k6/http
for seamless mocking.
import http, { mock } from "k6/x/mock"
-
Create separated
mock.js
module for mocking -
Re-export
k6/x/mock
module contentexport { default } from "k6/x/mock" export * from "k6/x/mock"
-
Put mock definitions in
mock.js
import { mock } from "k6/x/mock" mock('https://httpbin.test.k6.io/get', app => { app.get('/', (req, res) => { res.json({ url: 'https://httpbin.test.k6.io/get' }) }) })
-
In test script, import
http
frommock.js
instead ofk6/http
import http from "./mock.js";
Switching from mock to real implementation is as easy as replacing the line above with real
k6/http
module importimport http from "k6/http"
-
The other part of the test script is independent from mocking
import http from "./mock.js"; import { check } from 'k6' import { test } from 'k6/execution' export default async function () { const res = await http.asyncRequest('GET', 'https://httpbin.test.k6.io/get') const ok = check(res, { 'response code was 200': res => res.status == 200, '"url" was "https://httpbin.test.k6.io/get"': res => res.json('url') == 'https://httpbin.test.k6.io/get' }) if (!ok) { test.abort('unexpected response') } }
You can disable the given mock definition quickly by passing options parameter with skip
set to true.
mock(
'https://example.com',
app => {
app.get('/', (req, res) => {
res.json({ greeting: 'Hello World!' })
})
},
{ skip: true }
)
Alternatively you can put .skip
after function name:
mock.skip('https://example.com', app => {
app.get('/', (req, res) => {
res.json({ greeting: 'Hello World!' })
})
})
The API documentation bellow was generated from index.d.ts file.
An application object represents a web application.
The following example starts a server and listens for connections on port 3000. The application responds with a JSON object for requests to the root URL. All other routes are answered with a 404 not found message.
In this example, the name of the constructor is Application
, but the name you use is up to you.
Example
const app = new Application()
app.get('/', (req, res) => {
res.json({message:"Hello World!"})
})
app.listen(3000)
• new Application()
Creates a new application instance.
▸ delete(path
, ...middleware
): void
Routes HTTP DELETE
requests to the specified path with the specified middleware functions.
You can provide multiple middleware functions.
Name | Type | Description |
---|---|---|
path |
string |
The path for which the middleware function is invoked (string or path pattern) |
...middleware |
Middleware [] |
Middleware functions |
void
▸ get(path
, ...middleware
): void
Routes HTTP GET requests to the specified path with the specified middleware functions.
You can provide multiple middleware functions.
Name | Type | Description |
---|---|---|
path |
string |
The path for which the middleware function is invoked (string or path pattern) |
...middleware |
Middleware [] |
Middleware functions |
void
▸ head(path
, ...middleware
): void
Routes HTTP HEAD requests to the specified path with the specified middleware functions.
You can provide multiple middleware functions.
Name | Type | Description |
---|---|---|
path |
string |
The path for which the middleware function is invoked (string or path pattern) |
...middleware |
Middleware [] |
Middleware functions |
void
▸ listen(addr?
, callback?
): void
Starts the server.
Name | Type | Description |
---|---|---|
addr? |
string |
- |
callback? |
() => void |
host name or IP address for listening on, default 127.0.0.1 |
void
The instance for fluent/chaining API
▸ options(path
, ...middleware
): void
Routes HTTP OPTIONS requests to the specified path with the specified middleware functions.
You can provide multiple middleware functions.
Name | Type | Description |
---|---|---|
path |
string |
The path for which the middleware function is invoked (string or path pattern) |
...middleware |
Middleware [] |
Middleware functions |
void
▸ patch(path
, ...middleware
): void
Routes HTTP PATCH requests to the specified path with the specified middleware functions.
You can provide multiple middleware functions.
Name | Type | Description |
---|---|---|
path |
string |
The path for which the middleware function is invoked (string or path pattern) |
...middleware |
Middleware [] |
Middleware functions |
void
▸ post(path
, ...middleware
): void
Routes HTTP POST requests to the specified path with the specified middleware functions.
You can provide multiple middleware functions.
Name | Type | Description |
---|---|---|
path |
string |
The path for which the middleware function is invoked (string or path pattern) |
...middleware |
Middleware [] |
Middleware functions |
void
▸ put(path
, ...middleware
): void
Routes HTTP PUT requests to the specified path with the specified middleware functions.
You can provide multiple middleware functions.
Name | Type | Description |
---|---|---|
path |
string |
The path for which the middleware function is invoked (string or path pattern) |
...middleware |
Middleware [] |
Middleware functions |
void
▸ static(path
, docroot
): void
Mount static web content from given source directory.
Name | Type | Description |
---|---|---|
path |
string |
The path where the source will be mounted on |
docroot |
string |
The source directory path |
void
▸ use(path
, ...middleware
): void
Uses the specified middleware function or functions.
Name | Type | Description |
---|---|---|
path |
string |
The path for which the middleware function is invoked (string or path pattern) |
...middleware |
Middleware [] |
Middleware functions |
void
Optional flags for mock
function.
Passing this options object as first (or last) parameter to mock
function (or to Application
constructor) may
change its behavior.
Example
mock("https://example.com", callback, { sync:true });
• skip: boolean
True value indicaes that given mock definition should be ignored.
• sync: boolean
True value indicates synchronous mode operation. You should use it for synchronous k6 http API.
The req
object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on.
In this documentation and by convention, the object is always referred to as req
(and the HTTP response is res
) but its actual name is determined by the parameters to the callback function in which you’re working.
Example
app.get("/user/:id", function (req, res) {
res.send("user " + req.params.id);
});
• body: Record
<string
, any
>
Contains key-value pairs of data submitted in the request body.
By default, it is undefined, and is populated when the request
Content-Type is application/json
.
• cookies: Record
<string
, string
>
This property is an object that contains cookies sent by the request.
• get: (field
: string
) => string
▸ (field
): string
Returns the specified HTTP request header field (case-insensitive match).
Name | Type | Description |
---|---|---|
field |
string |
the header field name |
string
the header field value.
• header: (field
: string
) => string
▸ (field
): string
Returns the specified HTTP request header field (case-insensitive match).
Name | Type | Description |
---|---|---|
field |
string |
the header field name |
string
the header field value.
• method: string
Contains a string corresponding to the HTTP method of the request: GET, POST, PUT, and so on.
• params: Record
<string
, string
>
This property is an object containing properties mapped to the named route parameters. For example, if you have the route /user/:name, then the “name” property is available as req.params.name. This object defaults to empty.
• path: string
Contains the path part of the request URL.
• protocol: string
Contains the request protocol string: either http or (for TLS requests) https.
• query: Record
<string
, any
>
This property is an object containing a property for each query string parameter in the route.
For example:
// GET /search?q=tobi+ferret
console.dir(req.query.q);
// => 'tobi ferret'
// GET /shoes?color=blue&color=black&color=red
console.dir(req.query.color);
// => ['blue', 'black', 'red']
The res
object represents the HTTP response that a server sends when it gets an HTTP request.
In this documentation and by convention, the object is always referred to as res
(and the HTTP request is req
) but its actual name is determined by the parameters to the callback function in which you’re working.
Example
app.get("/user/:id", function (req, res) {
res.send("user " + req.params.id);
});
• append: (field
: string
, value
: string
) => Response
▸ (field
, value
): Response
Appends the specified value to the HTTP response header field. If the header is not already set, it creates the header with the specified value.
Name | Type | Description |
---|---|---|
field |
string |
the header field name |
value |
string |
the value to append |
• binary: (body
: string
| number
[] | ArrayBuffer
) => Response
▸ (body
): Response
Sends a binray response. This method sends a response (with the "application/octet-stream" content-type) that is the body paramter.
Name | Type | Description |
---|---|---|
body |
string | number [] | ArrayBuffer |
the data to send |
• html: (body
: string
) => Response
▸ (body
): Response
Sends a HTML text response. This method sends a response (with the correct content-type) that is the body string paramter.
Name | Type | Description |
---|---|---|
body |
string |
the string to send |
• json: (body
: Record
<string
, any
>) => Response
▸ (body
): Response
Sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string.
Name | Type | Description |
---|---|---|
body |
Record <string , any > |
the object to send |
• redirect: (code
: number
, loc
: string
) => Response
▸ (code
, loc
): Response
Redirects to the URL, with specified status, a positive integer that corresponds to an HTTP status code.
Name | Type | Description |
---|---|---|
code |
number |
the HTTP status code (301, 302, ...) |
loc |
string |
the location to redirect |
• send: (body
: string
| number
[] | ArrayBuffer
) => Response
▸ (body
): Response
Sends the HTTP response.
When the parameter is a ArrayBuffer or number[], the method sets the Content-Type response header field to “application/octet-stream”. When the parameter is a String, the method sets the Content-Type to “text/html”. Otherwise the method sets the Content-Type to "application/json" and convert paramter to JSON representation before sending.
Name | Type | Description |
---|---|---|
body |
string | number [] | ArrayBuffer |
the data to send |
• set: (field
: string
, value
: string
) => Response
▸ (field
, value
): Response
Sets the response’s HTTP header field to value.
Name | Type | Description |
---|---|---|
field |
string |
the header field name |
value |
string |
the value to set |
• status: (code
: number
) => Response
▸ (code
): Response
Sets the HTTP status for the response.
Name | Type | Description |
---|---|---|
code |
number |
the satus code value |
• text: (format
: string
, v?
: any
[]) => Response
▸ (format
, v?
): Response
Sends a plain text response. This method sends a response (with the correct content-type) that is the string formatting result.
Name | Type | Description |
---|---|---|
format |
string |
go format string |
v? |
any [] |
format values (if any) |
• type: (mime
: string
) => Response
▸ (mime
): Response
Sets the Content-Type HTTP header to the MIME type as from mime parameter.
Params
mime the content type
Name | Type |
---|---|
mime |
string |
• vary: (header
: string
) => Response
▸ (header
): Response
Adds the header field to the Vary response header.
Name | Type | Description |
---|---|---|
header |
string |
the header filed name |
Ƭ Middleware: (req
: Request
, res
: Response
, next
: () => void
) => void
▸ (req
, res
, next
): void
Middleware defines middleware and request handler callback function.
Name | Type | Description |
---|---|---|
req |
Request |
the request object |
res |
Response |
the response object |
next |
() => void |
calling from middleware enables processing next middleware |
void
▸ mock(target
, callback
, options?
): void
Create URL mock definition.
This function will create a new Express.js like web application and pass it to the provided callback function for programming. After mock programming is done, new mock HTTP server will be start. Whan you use http API from mock module, all matching URLs will be directed to this mock server.
You can create as many mock definitions (server) as you want.
You can disable the given mock definition quickly by passing options parameter with skip
set to true.
mock(
'https://example.com',
app => {
app.get('/', (req, res) => {
res.json({ greeting: 'Hello World!' })
})
},
{ skip: true }
)
Alternatively you can put .skip
after function name:
mock.skip('https://example.com', app => {
app.get('/', (req, res) => {
res.json({ greeting: 'Hello World!' })
})
})
Name | Type | Description |
---|---|---|
target |
String |
the URL or URL prefix to be mocked |
callback |
(app : Application ) => void |
function to for defining route definitions for mock server |
options? |
MockOptions |
optional flags (sync , skip ) |
void
▸ unmock(target
): void
Deactivate URL mocking.
This function will remove mock definition associated to given URL and stop the related HTTP server.
Name | Type | Description |
---|---|---|
target |
String |
the URL or URL prefix of mock definition to be remove |
void