Skip to content

Commit

Permalink
Merge pull request #1833 from grafana/replace-httpbin
Browse files Browse the repository at this point in the history
Replace httpbin references with QuickPizza
  • Loading branch information
federicotdn authored Jan 15, 2025
2 parents 78aeac2 + 4bf22a9 commit 4d5adff
Show file tree
Hide file tree
Showing 203 changed files with 1,122 additions and 1,018 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/run-code-blocks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches:
- main
paths:
- 'docs/sources/k6/next/**'
- 'docs/sources/k6/next/**/*.md'

jobs:
run-code-blocks:
Expand All @@ -17,8 +17,11 @@ jobs:
uses: tj-actions/changed-files@v45
with:
files: |
**.md
docs/sources/k6/next/**/*.md
- uses: grafana/setup-k6-action@v1
with:
browser: true
k6-version: '0.56.0'
- uses: actions/setup-python@v5
with:
python-version: '3.13'
Expand All @@ -27,6 +30,5 @@ jobs:
ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |
for file in ${ALL_CHANGED_FILES}; do
python -u scripts/md-k6.py "$file"
echo
python -u scripts/md-k6.py --duration 2s "$file"
done
22 changes: 21 additions & 1 deletion CONTRIBUTING/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ You can control the behaviour of `md-k6.py` via magic `md-k6` HTML comments plac

That is, `md-k6:` followed by a comma-separated list of options.

Currently, the only option that exists is `skip`, which will cause `md-k6.py` to ignore the code snippet completely (i.e. `<!-- md-k6:skip -->`). This is useful for code snippets that only showcase a very specific aspect of k6 scripting and do not contain an actually fully working script.
### `skip` Option

The `skip` option will cause `md-k6.py` to ignore the code snippet completely (i.e. `<!-- md-k6:skip -->`). This is useful for code snippets that only showcase a very specific aspect of k6 scripting and do not contain an actually fully working script.

> [!TIP]
> You can combine both `md-k6.py` and ESLint skip directives by placing the `md-k6.py` directive first:
Expand All @@ -198,6 +200,24 @@ Currently, the only option that exists is `skip`, which will cause `md-k6.py` to
> ```
> ````
### `skipall` Option
The `skipall` option will cause `md-k6.py` to ignore the entire Markdown file. Note that this option is special: it can be specified anywhere in the file, for example, at the very end. It also does not need to be placed above a code snippet. In order for this option to be read correctly, it must be specified alone, with no other additional options in the same HTML comment tag.
### `nofail` Option
The `nofail` option will allow the k6 code snippet to freely log errors without failing. However, if k6 exits with a nonzero status, the `md-k6.py` script will still fail. For this reason, using `nofail` will provide slightly better coverage than simply using `skip` on a code snippet.
### `env.X=Y` Option
Any option taking the form of `env.KEY=VALUE` will be parsed by the `md-k6.py` script, and the corresponding `KEY=VALUE` pairing will be added to the environment variables when executing the k6 code snippet. Note that for `KEY` and `VALUE` the following characters are **not** allowed: `,`, `-`, and `$`.
### `fixedscenarios` Option
By default, all code snippets are run with whatever scenarios they define via their `options` variable. However, some command line arguments to `md-k6.py` can change this, for example: `-d`/`--duration`. This option replaces the scenarios for all code snippets run with a scenario lasting only a specific amount of time. However, this behavior may break some scripts, for this reason, it is possible to specify the `fixedscenarios` option to ensure that the code snippet scenarios will be used as they appear in the Markdown file.
### Usage
To run the `md-k6.py` script locally, invoke it using Python. For example:
```bash
Expand Down
28 changes: 14 additions & 14 deletions docs/sources/k6/next/examples/cookies-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { check, group } from 'k6';

export default function () {
// Since this request redirects the `res.cookies` property won't contain the cookies
const res = http.get('https://httpbin.test.k6.io/cookies/set?name1=value1&name2=value2');
const res = http.post('https://quickpizza.grafana.com/api/cookies?name1=value1&name2=value2');
check(res, {
'status is 200': (r) => r.status === 200,
});
Expand Down Expand Up @@ -97,17 +97,17 @@ export default function () {
// that a request must match (domain, path, HTTPS or not etc.)
// to have the cookie attached to it when sent to the server.
const jar = http.cookieJar();
jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie', 'hello world', {
domain: 'httpbin.test.k6.io',
path: '/cookies',
jar.set('https://quickpizza.grafana.com/api/cookies', 'my_cookie', 'hello world', {
domain: 'quickpizza.grafana.com',
path: '/api/cookies',
secure: true,
max_age: 600,
});

// As the following request is matching the above cookie in terms of domain,
// path, HTTPS (secure) and will happen within the specified "age" limit, the
// cookie will be attached to this request.
const res = http.get('https://httpbin.test.k6.io/cookies');
const res = http.get('https://quickpizza.grafana.com/api/cookies');
check(res, {
'has status 200': (r) => r.status === 200,
"has cookie 'my_cookie'": (r) => r.json().cookies.my_cookie !== null,
Expand All @@ -130,10 +130,10 @@ import { check } from 'k6';

export default function () {
const jar = http.cookieJar();
jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie_1', 'hello world_1');
jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie_2', 'hello world_2');
jar.set('https://quickpizza.grafana.com/api/cookies', 'my_cookie_1', 'hello world_1');
jar.set('https://quickpizza.grafana.com/api/cookies', 'my_cookie_2', 'hello world_2');

const res1 = http.get('https://httpbin.test.k6.io/cookies');
const res1 = http.get('https://quickpizza.grafana.com/api/cookies');
check(res1, {
'res1 has status 200': (r) => r.status === 200,
"res1 has cookie 'my_cookie_1'": (r) => r.json().cookies.my_cookie_1 !== null,
Expand All @@ -142,9 +142,9 @@ export default function () {
'res1 cookie has correct value_2': (r) => r.json().cookies.my_cookie_2 == 'hello world_2',
});

jar.delete('https://httpbin.test.k6.io/cookies', 'my_cookie_1');
jar.delete('https://quickpizza.grafana.com/api/cookies', 'my_cookie_1');

const res2 = http.get('https://httpbin.test.k6.io/cookies');
const res2 = http.get('https://quickpizza.grafana.com/api/cookies');
check(res2, {
'res2 has status 200': (r) => r.status === 200,
"res2 hasn't cookie 'my_cookie_1'": (r) => r.json().cookies.my_cookie_1 == null,
Expand All @@ -168,17 +168,17 @@ import { check } from 'k6';

export default function () {
const jar = http.cookieJar();
jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie', 'hello world');
const res1 = http.get('https://httpbin.test.k6.io/cookies');
jar.set('https://quickpizza.grafana.com/api/cookies', 'my_cookie', 'hello world');
const res1 = http.get('https://quickpizza.grafana.com/api/cookies');
check(res1, {
'has status 200': (r) => r.status === 200,
"has cookie 'my_cookie'": (r) => r.json().cookies.my_cookie !== null,
'cookie has correct value': (r) => r.json().cookies.my_cookie == 'hello world',
});

jar.clear('https://httpbin.test.k6.io/cookies');
jar.clear('https://quickpizza.grafana.com/api/cookies');

const res2 = http.get('https://httpbin.test.k6.io/cookies');
const res2 = http.get('https://quickpizza.grafana.com/api/cookies');
check(res2, {
'has status 200': (r) => r.status === 200,
"hasn't cookie 'my_cookie'": (r) => r.json().cookies.my_cookie == null,
Expand Down
27 changes: 16 additions & 11 deletions docs/sources/k6/next/examples/correlation-and-dynamic-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,23 @@ import { check } from 'k6';

export default function () {
// Make a request that returns some JSON data
const res = http.get('https://httpbin.test.k6.io/json');
const reqHeaders = {
Authorization: 'Token abcdef0123456789',
};
const res = http.get('https://quickpizza.grafana.com/api/doughs', {
headers: reqHeaders,
});

// Extract data from that JSON data by first parsing it
// using a call to "json()" and then accessing properties by
// navigating the JSON data as a JS object with dot notation.
const slide1 = res.json().slideshow.slides[0];
check(slide1, {
'slide 1 has correct title': (s) => s.title === 'Wake up to WonderWidgets!',
'slide 1 has correct type': (s) => s.type === 'all',
const dough1 = res.json().doughs[0];
check(dough1, {
'dough1 1 has correct name': (s) => s.name === 'Thin',
'dough1 1 has correct ID': (s) => s.ID === 1,
});

// Now we could use the "slide1" variable in subsequent requests...
// Now we could use the "dough1" variable in subsequent requests...
}
```

Expand Down Expand Up @@ -130,13 +135,13 @@ import http from 'k6/http';

export default function () {
// This request returns XML:
const res = http.get('https://httpbin.test.k6.io/xml');
const res = http.get('https://quickpizza.grafana.com/api/xml?color=green');

// Use findBetween to extract the first title encountered:
const title = findBetween(res.body, '<title>', '</title>');
// Use findBetween to extract the first <value> tag encountered:
const color = findBetween(res.body, '<value>', '</value>');

check(title, {
'title is correct': (t) => t === 'Wake up to WonderWidgets!',
check(color, {
'color is correct': (t) => t === 'green',
});
}
```
Expand Down
4 changes: 3 additions & 1 deletion docs/sources/k6/next/examples/data-uploads.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export default function () {
fd.append('images', http.file(img2, 'image2.jpg', 'image/jpeg'));
fd.append('text', http.file(txt, 'text.txt', 'text/plain'));

const res = http.post('https://httpbin.test.k6.io/post', fd.body(), {
const res = http.post('https://quickpizza.grafana.com/api/post', fd.body(), {
headers: { 'Content-Type': 'multipart/form-data; boundary=' + fd.boundary },
});
check(res, {
Expand All @@ -148,3 +148,5 @@ Note that:
- Blob is not supported or implemented. For the same functionality, use
a simple object with the fields `data`, `content_type` (defaulting to "application/octet-stream") and optionally
`filename` as shown for `aBinaryFile` above.

<!-- md-k6:skipall -->
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ This script gets the request duration timing for a specific GET request and logs
import http from 'k6/http';

export default function () {
const res = http.get('https://httpbin.test.k6.io');
const res = http.get('https://quickpizza.grafana.com/');
console.log('Response time was ' + String(res.timings.duration) + ' ms');
}
```
Expand Down
4 changes: 2 additions & 2 deletions docs/sources/k6/next/examples/html-forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import { sleep } from 'k6';

export default function () {
// Request page containing a form
let res = http.get('https://httpbin.test.k6.io/forms/post');
let res = http.get('https://quickpizza.grafana.com/admin');

// Now, submit form setting/overriding some fields of the form
res = res.submitForm({
formSelector: 'form',
fields: { custname: 'test', extradata: 'test2' },
fields: { username: 'admin', password: 'admin' },
});
sleep(3);
}
Expand Down
42 changes: 5 additions & 37 deletions docs/sources/k6/next/examples/http-authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function () {

// Passing username and password as part of the URL will
// allow us to authenticate using HTTP Basic Auth.
const url = `https://${credentials}@httpbin.test.k6.io/basic-auth/${username}/${password}`;
const url = `https://${credentials}@quickpizza.grafana.com/api/basic-auth/${username}/${password}`;

let res = http.get(url);

Expand All @@ -45,9 +45,9 @@ export default function () {
},
};

res = http.get(`https://httpbin.test.k6.io/basic-auth/${username}/${password}`, options);
res = http.get(`https://quickpizza.grafana.com/api/basic-auth/${username}/${password}`, options);

// Verify response (checking the echoed data from the httpbin.test.k6.io
// Verify response (checking the echoed data from the QuickPizza
// basic auth test API endpoint)
check(res, {
'status is 200': (r) => r.status === 200,
Expand All @@ -59,40 +59,6 @@ export default function () {

{{< /code >}}

## Digest authentication

{{< code >}}

```javascript
import http from 'k6/http';
import { check } from 'k6';

const username = 'user';
const password = 'passwd';

export default function () {
// Passing username and password as part of URL plus the auth option will
// authenticate using HTTP Digest authentication.
const credentials = `${username}:${password}`;
const res = http.get(
`https://${credentials}@httpbin.test.k6.io/digest-auth/auth/${username}/${password}`,
{
auth: 'digest',
}
);

// Verify response (checking the echoed data from the httpbin.test.k6.io digest auth
// test API endpoint)
check(res, {
'status is 200': (r) => r.status === 200,
'is authenticated': (r) => r.json().authenticated === true,
'is correct user': (r) => r.json().user === username,
});
}
```

{{< /code >}}

## NTLM authentication

{{< code >}}
Expand Down Expand Up @@ -121,6 +87,8 @@ Here's an example script to demonstrate how to sign a request to fetch an object

{{< code >}}

<!-- md-k6:skip -->

```javascript
import http from 'k6/http';
import {
Expand Down
1 change: 0 additions & 1 deletion docs/sources/k6/next/get-started/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ If you need a place to learn k6 and test your scripts, you can use these playgro

- [test-api.k6.io](https://test-api.k6.io/). A simple REST and WebSocket web application. [grafana/test-api.k6.io](https://github.com/grafana/test-api.k6.io)
- [grafana/quickpizza](https://github.com/grafana/quickpizza). A simple demo web application.
- [grafana/httpbin](https://github.com/grafana/httpbin). A simple HTTP Request & Response Service.

Note that these are shared testing environments - please avoid high-load tests. Alternatively, you can deploy and host them on your infrastructure and run the examples in the repository.

Expand Down
2 changes: 1 addition & 1 deletion docs/sources/k6/next/get-started/running-k6.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export const options = {
};

export default function () {
const res = http.get('https://httpbin.test.k6.io/');
const res = http.get('https://quickpizza.grafana.com/');
check(res, { 'status was 200': (r) => r.status == 200 });
sleep(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Note, this method returns a Promise. You must use the `await` keyword to resolve
import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js';

const session = new Httpx({
baseURL: 'https://httpbin.test.k6.io',
baseURL: 'https://quickpizza.grafana.com/api',
timeout: 20000, // 20s timeout.
});

Expand Down
2 changes: 1 addition & 1 deletion docs/sources/k6/next/javascript-api/jslib/httpx/delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ weight: 14
import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js';

const session = new Httpx({
baseURL: 'https://httpbin.test.k6.io',
baseURL: 'https://quickpizza.grafana.com/api',
timeout: 20000, // 20s timeout.
});

Expand Down
4 changes: 2 additions & 2 deletions docs/sources/k6/next/javascript-api/jslib/httpx/head.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ weight: 16
import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js';

const session = new Httpx({
baseURL: 'https://httpbin.test.k6.io',
baseURL: 'https://quickpizza.grafana.com',
timeout: 20000, // 20s timeout.
});

export default function testSuite() {
const resp = session.head(`/head`);
const resp = session.head(`/`);
console.log(resp.status);
}
```
Expand Down
4 changes: 2 additions & 2 deletions docs/sources/k6/next/javascript-api/jslib/httpx/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ weight: 15
import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js';

const session = new Httpx({
baseURL: 'https://httpbin.test.k6.io',
baseURL: 'https://quickpizza.grafana.com',
timeout: 20000, // 20s timeout.
});

export default function testSuite() {
const resp = session.options(`/options`);
const resp = session.options(`/`);
console.log(resp.status);
}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/sources/k6/next/javascript-api/jslib/httpx/patch.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ weight: 13
import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js';

const session = new Httpx({
baseURL: 'https://httpbin.test.k6.io',
baseURL: 'https://quickpizza.grafana.com/api',
timeout: 20000, // 20s timeout.
});

Expand Down
2 changes: 1 addition & 1 deletion docs/sources/k6/next/javascript-api/jslib/httpx/put.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ weight: 12
import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js';

const session = new Httpx({
baseURL: 'https://httpbin.test.k6.io',
baseURL: 'https://quickpizza.grafana.com/api',
timeout: 20000, // 20s timeout.
});

Expand Down
2 changes: 1 addition & 1 deletion docs/sources/k6/next/javascript-api/jslib/httpx/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Consider using specific methods for making common requests [get](https://grafana
import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js';

const session = new Httpx({
baseURL: 'https://httpbin.test.k6.io',
baseURL: 'https://quickpizza.grafana.com/api',
timeout: 20000, // 20s timeout.
});

Expand Down
Loading

0 comments on commit 4d5adff

Please sign in to comment.