Skip to content

Commit 7d367f7

Browse files
committed
feat(Agent Configuration): Allow GOT config
- Allow underlying got http client to be configured via client
1 parent 6923355 commit 7d367f7

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

lib/agent.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export class Agent implements IAgent {
7474
throwHttpErrors: false,
7575
body,
7676
timeout,
77+
...this.gotConfig,
7778
})
7879
.then(response => toHttpResponse(httpRequest, response))
7980
.catch(handleError);
@@ -88,6 +89,7 @@ export class Agent implements IAgent {
8889
timeout,
8990
throwHttpErrors: false,
9091
json: true,
92+
...this.gotConfig,
9193
})
9294
.then(response => toHttpResponse(httpRequest, response))
9395
.catch(handleError);

test/agent.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,81 @@ describe("Agent", () => {
142142
timeout: 1000,
143143
} as any);
144144
});
145+
146+
describe("GOT Configuration", () => {
147+
const timeout = 2000;
148+
const retry = 2;
149+
150+
beforeEach(() => {
151+
agent = new Agent({ timeout, retry });
152+
});
153+
154+
it("overrides HTTP configuration for GET requests", async () => {
155+
const method: HttpVerb = "GET";
156+
const query = { foo: "bar" };
157+
const header = { baz: "quux" };
158+
const url = "http://www.foo.com/";
159+
const SUCCESS = 200;
160+
161+
const response: unknown = {
162+
statusCode: SUCCESS,
163+
headers: header,
164+
body: Buffer.from("{}"),
165+
url,
166+
};
167+
168+
const stub = sinon
169+
.stub(agent, "got")
170+
.resolves(response as Response<Buffer>);
171+
172+
await agent.http({ method, timeout: 1000, url, header, query });
173+
174+
sinon.assert.calledOnce(stub);
175+
sinon.assert.calledWithExactly(stub, url, {
176+
method,
177+
headers: header,
178+
throwHttpErrors: false,
179+
query,
180+
json: true,
181+
timeout,
182+
retry,
183+
} as any);
184+
});
185+
186+
it("overrides HTTP configuration for POST requests", async () => {
187+
const method: HttpVerb = "POST";
188+
const query = { foo: "bar" };
189+
const header = { baz: "quux" };
190+
const url = "http://www.foo.com/";
191+
const SUCCESS = 200;
192+
const body = { foo: "bar" };
193+
194+
const response: unknown = {
195+
statusCode: SUCCESS,
196+
headers: header,
197+
body: Buffer.from("{}"),
198+
url,
199+
};
200+
201+
const stub = sinon
202+
.stub(agent, "got")
203+
.resolves(response as Response<Buffer>);
204+
205+
await agent.http({ body, method, timeout: 1000, url, header, query });
206+
207+
sinon.assert.calledOnce(stub);
208+
sinon.assert.calledWithExactly(stub, url, {
209+
method,
210+
throwHttpErrors: false,
211+
json: true,
212+
body,
213+
headers: header,
214+
query,
215+
retry,
216+
timeout,
217+
} as any);
218+
});
219+
});
145220
});
146221

147222
describe("Error handling", () => {

0 commit comments

Comments
 (0)