test_generator
Runs a code generator's source once without saving it.
Overview
test_generator runs JavaScript generator source and returns everything it produced, without saving anything. An agent can call it as often as it needs while it writes a generator, and save the finished one once with create_generator.
Unless you name a request, the generator runs once for each of several sample requests, so each of its branches runs:
- HTTP: a JSON POST, a GET with bearer auth, a form with Basic auth and settings that turn off redirects and certificate checks, a multipart upload with a file part and cookies, and a PROPFIND with a raw XML body.
- WebSocket: a secure connection with bearer auth, a subprotocol and saved messages, and a local one with Basic auth.
{
"source": "var manifest = { id: 'httpie-short', name: 'HTTPie (short)', group: 'Shell', language: 'shell', fileExtension: 'sh' };\n\nfunction generate(req, r) {\n var b = r.builder(' ');\n b.add('http').add(req.method).add(r.q.shell(req.url));\n r.allHeaders(req).forEach(function (h) { b.add(r.q.shell(h.name + ':' + h.value)); });\n return b.join();\n}\n"
}The source is written as for create_generator, except that the manifest's id can be left out.
Parameters
Prop
Type
Return Value
{
"id": "httpie-short",
"name": "HTTPie (short)",
"kind": "http",
"language": "shell",
"fileExtension": "sh",
"testedWith": ["JSON POST", "GET with bearer auth", "Form with Basic auth", "Multipart with cookies", "Custom method with a raw body"],
"runs": [
{
"request": "JSON POST",
"code": "http POST 'https://api.example.com/v1/items?verbose=true' 'Accept:application/json' 'User-Agent:Relay' 'Content-Type:application/json'",
"logs": [],
"warnings": ["The request has a json body, but none of it appears in the output. …"]
}
],
"warnings": ["JSON POST: The request has a json body, but none of it appears in the output. …"],
"verificationNote": "Relay ran the generator and checked the text of its output. It didn't compile or run the generated code, …"
}Prop
Type
Discussion
A generator only fails when it throws or returns something other than a string, so warnings points out output that is probably wrong anyway:
- The text
undefined,[object Object]orNaN, which usually means the generator read a field the request doesn't have. - A body, host or method that never appears in the output.
- A whole program on one line with literal
\nsequences, which comes from joining lines with'\\n'instead of'\n'. - An empty output.
Relay doesn't compile or run the generated code, so output with no warnings can still fail to compile.
The tool returns an error, naming the requests it failed on and the line number when there is one, when the generator throws, fails to parse, returns something other than a string, or runs too long. It also returns an error when the source is empty or larger than 512 KB, or when testRequestId names a request of the other kind.
See Also
- create_generator: Saves a new JavaScript code generator.
- get_generator: Returns a code generator's manifest and JavaScript source.
- Code Generator API: The manifest, request model and helpers for writing code generators.