Skip to content

Commit

Permalink
Remove httpbin references (75 remain)
Browse files Browse the repository at this point in the history
  • Loading branch information
federicotdn committed Jan 9, 2025
1 parent d850845 commit 1b9b322
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 69 deletions.
2 changes: 1 addition & 1 deletion docs/sources/k6/next/javascript-api/jslib/utils/check.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function waitFor(delay) {
}

export default async function () {
const res = http.get('https://httpbin.test.k6.io');
const res = http.get('http://quickpizza.grafana-dev.com:3333/');

const success = await check(res, {
'passing promise as a value': waitFor(1000),
Expand Down
16 changes: 13 additions & 3 deletions docs/sources/k6/next/javascript-api/k6-data/sharedarray.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ When a script requests an element, k6 gives a _copy_ of that element.
You must construct a `SharedArray` in the [`init` context](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/test-lifecycle).
Its constructor takes a name for the `SharedArray` and a function that needs to return an array object itself:

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

```javascript
import { SharedArray } from 'k6/data';

Expand Down Expand Up @@ -49,6 +51,8 @@ This limitation will eventually be removed, but for now, the implication is that

{{< code >}}

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

```javascript
import { SharedArray } from 'k6/data';

Expand Down Expand Up @@ -79,6 +83,8 @@ To test this, we ran the following script on version v0.31.0 with 100 VUs.

{{< code >}}

<!-- md-k6:env.N=3 -->

```javascript
import { check } from 'k6';
import http from 'k6/http';
Expand All @@ -102,9 +108,13 @@ if (__ENV.SHARED === 'true') {

export default function () {
const iterationData = data[Math.floor(Math.random() * data.length)];
const res = http.post('https://httpbin.test.k6.io/anything', JSON.stringify(iterationData), {
headers: { 'Content-type': 'application/json' },
});
const res = http.post(
'http://quickpizza.grafana-dev.com:3333/api/post',
JSON.stringify(iterationData),
{
headers: { 'Content-type': 'application/json' },
}
);
check(res, { 'status 200': (r) => r.status === 200 });
}
```
Expand Down
15 changes: 9 additions & 6 deletions docs/sources/k6/next/javascript-api/k6-http/cookiejar/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,22 @@ import http from 'k6/http';
import { check } from 'k6';

export default function () {
const res1 = http.get('https://httpbin.test.k6.io/cookies/set?my_cookie=hello%20world', {
redirects: 0,
});
const res1 = http.post(
'http://quickpizza.grafana-dev.com:3333/api/cookies?my_cookie=hello%20world',
{
redirects: 0,
}
);
const jar = http.cookieJar();
const cookies = jar.cookiesForURL('https://httpbin.test.k6.io/');
const cookies = jar.cookiesForURL('http://quickpizza.grafana-dev.com:3333/api/cookies');
check(res1, {
"has cookie 'my_cookie'": (r) => cookies.my_cookie.length > 0,
'cookie has correct value': (r) => cookies.my_cookie[0] === 'hello world',
});

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

const res2 = http.get('https://httpbin.test.k6.io/cookies');
const res2 = http.get('http://quickpizza.grafana-dev.com:3333/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
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ description: 'Delete all cookies for the given URL.'
import http from 'k6/http';

export default function () {
http.get('https://httpbin.test.k6.io/cookies/set?one=1&two=2');
http.post('http://quickpizza.grafana-dev.com:3333/api/cookies?one=1&two=2');

// We'll use httpbin's reflection to see what cookies we
// We'll use QuickPizza's cookie reflection to see what cookies we
// are actually sending to the server after every change
let httpbinResp;
httpbinResp = http.get('https://httpbin.test.k6.io/cookies');
console.log(JSON.stringify(httpbinResp.json().cookies));
let qpResp;
qpResp = http.get('http://quickpizza.grafana-dev.com:3333/api/cookies');
console.log(JSON.stringify(qpResp.json().cookies));
// Will print '{"one":"1","two":"2"}'

const jar = http.cookieJar(); // get the VU specific jar
jar.set('https://httpbin.test.k6.io/cookies', 'three', '3');
httpbinResp = http.get('https://httpbin.test.k6.io/cookies');
console.log(JSON.stringify(httpbinResp.json().cookies));
jar.set('http://quickpizza.grafana-dev.com:3333/api/cookies', 'three', '3');
qpResp = http.get('http://quickpizza.grafana-dev.com:3333/api/cookies');
console.log(JSON.stringify(qpResp.json().cookies));
// Will print '{"one":"1","three":"3","two":"2"}'

jar.delete('https://httpbin.test.k6.io/cookies', 'one');
httpbinResp = http.get('https://httpbin.test.k6.io/cookies');
console.log(JSON.stringify(httpbinResp.json().cookies));
jar.delete('http://quickpizza.grafana-dev.com:3333/api/cookies', 'one');
qpResp = http.get('http://quickpizza.grafana-dev.com:3333/api/cookies');
console.log(JSON.stringify(qpResp.json().cookies));
// Will print '{"three":"3","two":"2"}'

jar.clear('https://httpbin.test.k6.io/cookies');
httpbinResp = http.get('https://httpbin.test.k6.io/cookies');
console.log(JSON.stringify(httpbinResp.json().cookies));
jar.clear('http://quickpizza.grafana-dev.com:3333/api/cookies');
qpResp = http.get('http://quickpizza.grafana-dev.com:3333/api/cookies');
console.log(JSON.stringify(qpResp.json().cookies));
// Will print '{}'
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ import { check } from 'k6';

export default function () {
const jar = http.cookieJar();
jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie', 'hello world', {
domain: 'httpbin.test.k6.io',
path: '/cookies',
secure: true,
jar.set('http://quickpizza.grafana-dev.com', 'my_cookie', 'hello world', {
domain: 'quickpizza.grafana-dev.com',
path: '/api/cookies',
secure: false, // TODO change to secure: true
max_age: 600,
});
const res = http.get('https://httpbin.test.k6.io/cookies');
const res = http.get('http://quickpizza.grafana-dev.com:3333/api/cookies');
console.log(res.body);
check(res, {
'has status 200': (r) => r.status === 200,
"has cookie 'my_cookie'": (r) => r.json().cookies.my_cookie !== null,
Expand Down
33 changes: 3 additions & 30 deletions docs/sources/k6/next/javascript-api/k6-http/params.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ Here is another example using [http.batch()](https://grafana.com/docs/k6/<K6_VER
```javascript
import http from 'k6/http';

const url1 = 'https://api.k6.io/v3/account/me';
const url2 = 'https://httpbin.test.k6.io/get';
const url1 = 'http://quickpizza.grafana-dev.com:3333/api/get';
const url2 = 'http://quickpizza.grafana-dev.com:3333/api/delete';
const apiToken = 'f232831bda15dd233c53b9c548732c0197619a3d3c451134d9abded7eb5bb195';
const requestHeaders = {
'User-Agent': 'k6',
Expand All @@ -71,33 +71,6 @@ export default function () {

{{< /code >}}

### Example of Digest Authentication

Here is one example of how to use the `Params` to Digest Authentication.

{{< code >}}

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

export default function () {
// Passing username and password as part of URL plus the auth option will authenticate using HTTP Digest authentication
const res = http.get('http://user:[email protected]/digest-auth/auth/user/passwd', {
auth: 'digest',
});

// Verify response
check(res, {
'status is 200': (r) => r.status === 200,
'is authenticated': (r) => r.json().authenticated === true,
'is correct user': (r) => r.json().user === 'user',
});
}
```

{{< /code >}}

### Example of overriding discardResponseBodies

{{< code >}}
Expand All @@ -110,7 +83,7 @@ export default function () {}
export function setup() {
// Get 10 random bytes as an ArrayBuffer. Without the responseType the body
// will be null.
const response = http.get('https://httpbin.test.k6.io/bytes/10', {
const response = http.get('http://quickpizza.grafana-dev.com:3333/api/bytes/10', {
responseType: 'binary',
});
// response.body is an ArrayBuffer, so wrap it in a typed array view to access
Expand Down
4 changes: 3 additions & 1 deletion docs/sources/k6/next/javascript-api/k6-http/post.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ weight: 10

{{< code >}}

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

```javascript
import http from 'k6/http';

const url = 'https://httpbin.test.k6.io/post';
const url = 'http://quickpizza.grafana-dev.com:3333/api/json';
const logoBin = open('./logo.png', 'b');

export default function () {
Expand Down
6 changes: 3 additions & 3 deletions docs/sources/k6/next/javascript-api/k6-http/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Using http.request() to issue a POST request:
```javascript
import http from 'k6/http';

const url = 'https://httpbin.test.k6.io/post';
const url = 'http://quickpizza.grafana-dev.com:3333/api/post';

export default function () {
const data = { name: 'Bert' };
Expand All @@ -38,12 +38,12 @@ export default function () {
let res = http.request('POST', url, JSON.stringify(data), {
headers: { 'Content-Type': 'application/json' },
});
console.log(res.json().json.name); // Bert
console.log(res.json().name); // Bert

// Using an object as body, the headers will automatically include
// 'Content-Type: application/x-www-form-urlencoded'.
res = http.request('POST', url, data);
console.log(res.json().form.name); // Bert
console.log(res.body); // name=Bert
}
```

Expand Down
44 changes: 38 additions & 6 deletions scripts/md-k6.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
import tempfile
from collections import namedtuple

Script = namedtuple("Script", ["text", "options"])
Script = namedtuple("Script", ["text", "options", "env"])


def run_k6(script: Script, duration: str | None) -> None:
def run_k6(script: Script, duration: str | None, verbose: bool) -> None:
script_file = tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".js")
script_file.write(script.text)
script_file.close()
Expand All @@ -32,22 +32,38 @@ def run_k6(script: Script, duration: str | None) -> None:
script_file.name,
"--log-format=json",
f"--log-output=file={logs_file.name}",
"-w",
"-w", # Promote some warnings to errors.
]
if duration:
cmd.extend(["-d", duration])

result = subprocess.run(cmd)
env = {**os.environ, **script.env}
result = subprocess.run(cmd, env=env)

if result.returncode:
print("k6 returned non-zero status:", result.returncode)
try:
with open(logs_file.name) as f:
logs = f.read()

print("logs:")
print(logs)
except Exception:
# Ignore exceptions if we fail to read the logs
pass
exit(1)

with open(logs_file.name) as f:
lines = f.readlines()

# There's no way of running k6 OSS in a way that the process fails
# immediately after the first exception (unless this specific handling
# is added to the script explicitly). So here we just read the logs for
# any errors and fail if at least one was found.
for line in lines:
line = line.strip()
if verbose:
print(line)
parsed = json.loads(line)
if parsed["level"] == "error":
print("error in k6 script execution:", line)
Expand All @@ -70,6 +86,13 @@ def main() -> None:
parser.add_argument(
"--duration", "-d", default=None, help="Override script(s) duration."
)
parser.add_argument(
"--verbose",
"-v",
default=False,
help="Enable verbose mode. All log output for tests will be printed.",
action="store_true",
)
args = parser.parse_args()

print("Reading from file:", args.file.name)
Expand Down Expand Up @@ -121,7 +144,16 @@ def main() -> None:
if "skip" in options:
continue

scripts.append(Script(text="\n".join(lines[1:]), options=options))
env = {}
for opt in options:
if not opt.startswith("env."):
continue
if "=" not in opt:
opt += "="
key, value = opt.removeprefix("env.").split("=")
env[key] = value

scripts.append(Script(text="\n".join(lines[1:]), options=options, env=env))

range_parts = args.blocks.split(":")
try:
Expand All @@ -144,7 +176,7 @@ def main() -> None:
f"Running script #{i} (hash: {script_hash}, options: {script.options}):\n"
)
print(textwrap.indent(script.text, " "))
run_k6(script, args.duration)
run_k6(script, args.duration, args.verbose)
print()


Expand Down

0 comments on commit 1b9b322

Please sign in to comment.