Skip to content

Commit

Permalink
feat: add test for global import #416
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed Mar 17, 2023
1 parent 0b0001a commit 820b08d
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions src/test/metaData/import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { getLocal } from 'mockttp';

describe('metadata.import', () => {
const localServer = getLocal();
beforeEach(() => localServer.start(8005));
beforeEach(() => localServer.start(8008));
afterEach(() => localServer.stop());

it('name + import + ref', async () => {
initFileProvider({
'import.http': `
# @name foo
GET http://localhost:8005/json
GET http://localhost:8008/json
`,
});
await localServer.forGet('/json').thenJson(200, { foo: 'bar', test: 1 });
Expand All @@ -20,7 +20,7 @@ GET http://localhost:8005/json
# @import ./import.http
###
# @ref foo
POST http://localhost:8005/post?test={{foo.test}}
POST http://localhost:8008/post?test={{foo.test}}
foo={{foo.foo}}
`);
Expand All @@ -31,7 +31,7 @@ foo={{foo.foo}}
});

const requests = await mockedEndpoints.getSeenRequests();
expect(requests[0].url).toBe('http://localhost:8005/post?test=1');
expect(requests[0].url).toBe('http://localhost:8008/post?test=1');
expect(await requests[0].body.getText()).toBe('foo=bar');
});
it('import global variable', async () => {
Expand All @@ -45,7 +45,7 @@ foo={{foo.foo}}
const mockedEndpoints = await localServer.forPost('/post').thenReply(200);
const httpFile = await parseHttp(`
# @import ./import.http
POST http://localhost:8005/post?foo={{foo}}
POST http://localhost:8008/post?foo={{foo}}
bar={{bar}}
`);
Expand All @@ -56,7 +56,7 @@ bar={{bar}}
});

const requests = await mockedEndpoints.getSeenRequests();
expect(requests[0].url).toBe('http://localhost:8005/post?foo=bar');
expect(requests[0].url).toBe('http://localhost:8008/post?foo=bar');
expect(await requests[0].body.getText()).toBe('bar=foo');
});
it('import named variable', async () => {
Expand All @@ -73,7 +73,7 @@ bar={{bar}}
const httpFile = await parseHttp(`
# @import ./import.http
# @ref test
POST http://localhost:8005/post?foo={{foo}}
POST http://localhost:8008/post?foo={{foo}}
bar={{bar}}
`);
Expand All @@ -84,7 +84,42 @@ bar={{bar}}
});

const requests = await mockedEndpoints.getSeenRequests();
expect(requests[0].url).toBe('http://localhost:8005/post?foo=bar');
expect(requests[0].url).toBe('http://localhost:8008/post?foo=bar');
expect(await requests[0].body.getText()).toBe('bar=foo');
});
it('import request with using global host variable', async () => {
initFileProvider({
'import.http': `
@host=http://localhost:8008
### Apple
# @name send_apple
POST /anything
Content-Type: application/json
{
"id": "0001",
"type": "fruit",
"name": "Apple"
}
`,
});
await localServer.forGet('/json').thenJson(200, { foo: 'bar', test: 1 });
const mockedEndpoints = await localServer.forPost('/anything').thenReply(200);
const httpFile = await parseHttp(`
# @import ./import.http
###
# @forceRef send_apple
`);

await send({
httpFile,
httpRegion: httpFile.httpRegions[1],
});

const requests = await mockedEndpoints.getSeenRequests();
expect(requests[0].url).toBe('http://localhost:8008/anything');
expect(requests[0].headers['content-type']).toBe('application/json');
});
});

0 comments on commit 820b08d

Please sign in to comment.