-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a dedicated example for the
File.readAll
method
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import http from "k6/http"; | ||
import { open } from "k6/experimental/fs"; | ||
|
||
export const options = { | ||
vus: 1, | ||
iterations: 1, | ||
}; | ||
|
||
// As k6 does not support asynchronous code in the init context, yet, we need to | ||
// use a top-level async function to be able to use the `await` keyword. | ||
let file; | ||
(async function () { | ||
file = await open("bonjour.txt"); | ||
})(); | ||
|
||
export default async function () { | ||
// Obtain information about the file. | ||
const fileinfo = await file.stat(); | ||
if (fileinfo.name != "bonjour.txt") { | ||
throw new Error("Unexpected file name"); | ||
} | ||
|
||
let res = http.post( | ||
"https://httpbin.test.k6.io/post", | ||
await file.readAll(), | ||
{ | ||
headers: { "Content-Type": "text/plain" }, | ||
} | ||
); | ||
|
||
console.log(res.body); | ||
} |