Skip to content

Commit

Permalink
feat: allow custom http header
Browse files Browse the repository at this point in the history
  • Loading branch information
albertolerda committed Oct 26, 2023
1 parent 1ad444e commit 78b47d3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/core/test/slangroom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Given I have a 'string' named 'a'
Then print 'a'
Then I send a 'a' and B and output into 'b'
Then I pass a 'b' and c D
Then I send a 'b' and c D
Then I send a 'b' and C d and output into 'mimmo'
Then I open 'b' and e F
Then I connect to 'a' and F g
Expand Down
4 changes: 2 additions & 2 deletions pkg/core/test/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('generated ast is correct', async (t) => {
phrase: 'read the ethereum balance',
bindings: new Map<string, string>(),
},
"connect to 'foo' and pass address 'addr' and send contract 'contract' and read the ethereum balance":
"connect to 'foo' and send address 'addr' and send contract 'contract' and read the ethereum balance":
{
openconnect: 'foo',
phrase: 'read the ethereum balance',
Expand All @@ -34,7 +34,7 @@ test('generated ast is correct', async (t) => {
['contract', 'contract'],
]),
},
"open 'foo' and pass address 'addr' and send contract 'contract' and read the ethereum balance and output into 'var'":
"open 'foo' and send address 'addr' and send contract 'contract' and read the ethereum balance and output into 'var'":
{
openconnect: 'foo',
phrase: 'read the ethereum balance',
Expand Down
38 changes: 31 additions & 7 deletions pkg/http/src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { JsonableArray } from '@slangroom/shared';
import type { PluginContext, PluginResult } from '@slangroom/core';
import { lex, parse, visit, RequestKind, RequestMethod, type PhraseCst } from '@slangroom/http';
import axios from 'axios';
import axios, { type AxiosRequestConfig } from 'axios';

/**
* The default timeout of an HTTP request in milliseconds.
Expand Down Expand Up @@ -36,13 +36,18 @@ export const execute = async (
method: RequestMethod
): Promise<PluginResult> => {
const url = ctx.fetchConnect()[0];
const headers = ctx.get('headers');
if (kind === RequestKind.Default) {
let error: any = null;
const req = await request({
let requestData: AxiosRequestConfig = {
url: url,
method: method,
data: ctx.get('object'),
}).catch((e) => (error = e));
data: ctx.get('object') as any,
}
if(headers) {
requestData['headers'] = headers as any;
}
const req = await request(requestData).catch((e) => (error = e));
const zenResult = error
? { status: error.code, error: '' }
: { status: req.status, result: req.data || '' };
Expand All @@ -56,12 +61,31 @@ export const execute = async (
// TODO: check type of body (needs to be JsonableArray of
// JsonableObject)
const objects = ctx.fetch('object') as JsonableArray;
for (const [i, u] of urls.entries())
reqs.push(request({ url: u, method: method, data: objects[i] }));
for (const [i, u] of urls.entries()) {
let requestData: AxiosRequestConfig = {
url: u,
method: method,
data: objects[i],
}
if(headers) {
requestData['headers'] = headers as any;
}
reqs.push(request(requestData));
}
} else {
// TODO: check type of body (needs to be JsonableObject)
const object = ctx.fetch('object') as JsonableArray;
for (const u of urls) reqs.push(request({ url: u, method: method, data: object }));
for (const u of urls) {
let requestData: AxiosRequestConfig = {
url: u,
method: method,
data: object,
}
if(headers) {
requestData['headers'] = headers as any;
}
reqs.push(request(requestData));
}
}
const results: JsonableArray = new Array(reqs.length);
const errors: { [key: number]: any } = {};
Expand Down
33 changes: 33 additions & 0 deletions pkg/http/test/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ nock('http://localhost')
if (req?.includes('Hola') || req?.includes('Hi')) return [200, 'received result'];
return [500, 'Did not receive the result'];
})
.get('/auth-required')
.matchHeader('authorization', 'Basic Auth test')
.reply(200, 'Yes, you can!')
.persist();

test('Full script that uses http plugin', async (t) => {
Expand Down Expand Up @@ -56,3 +59,33 @@ Then I connect to 'final_endpoints' and send object 'string_array' and do parall
res.logs
);
});

test('Send auth header', async (t) => {
const script = `
Rule caller restroom-mw
Given I connect to 'auth_url' and send headers 'headers' and do get and output into 'auth'
Given I have a 'string dictionary' named 'auth'
Then print data
`;
const slangroom = new Slangroom(httpPlugins);
const res = await slangroom.execute(script, {
data: {
auth_url: 'http://localhost/auth-required',
headers: {
authorization: 'Basic Auth test'
}
},
});
t.deepEqual(
res.result,
{
auth: {
result: 'Yes, you can!',
status: 200,
},
},
res.logs
);
});

0 comments on commit 78b47d3

Please sign in to comment.