Skip to content

Commit a62fa61

Browse files
committed
migrate httpPut functionaity from old sdk to new
1 parent c9bb332 commit a62fa61

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

src/http/http-client.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export default interface IHttpClient {
6666
httpGet(url: string): Promise<HttpResponse>;
6767
httpPost(url: string, body?: any): Promise<HttpResponse>;
6868
httpPatch(url: string, body?: any, headers?: Headers): Promise<HttpResponse>;
69+
httpPut(url: string, body?: any, headers?: Headers): Promise<HttpResponse>;
6970
httpDelete(url: string): Promise<HttpResponse>;
7071
}
7172

@@ -127,6 +128,8 @@ export abstract class AbstractClient implements IHttpClient {
127128

128129
public abstract httpPatch(url: string, body?: any, headers?: Headers): Promise<HttpResponse>;
129130

131+
public abstract httpPut(url: string, body?: any, headers?: Headers): Promise<HttpResponse>;
132+
130133
public abstract httpDelete(url: string): Promise<HttpResponse>;
131134

132135
private init () {

src/http/request-client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export default class RequestClient extends AbstractClient {
1818
return this.request({ method: "PATCH", url, body, headers });
1919
}
2020

21+
public async httpPut (url: string, body?: any, headers?: Headers): Promise<HttpResponse> {
22+
return this.request({ method: "PUT", url, body, headers });
23+
}
24+
2125
public async httpDelete (url: string): Promise<HttpResponse> {
2226
return this.request({ method: "DELETE", url });
2327
}

test/http/http-client.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ class TestClient extends AbstractClient {
1111
throw new Error("Method not implemented.");
1212
}
1313

14+
public httpPut (url: string, body: any, headers: any): Promise<HttpResponse> {
15+
throw new Error("Method not implemented.");
16+
}
17+
1418
public httpDelete (url: string): Promise<HttpResponse> {
1519
throw new Error("Method not implemented.");
1620
}

test/http/request-client.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,42 @@ describe("request-client", () => {
124124
expect(resp.status).to.equal(statusCode);
125125
});
126126

127+
it("returns an error response when HTTP PUT request fails", async () => {
128+
const returnedBody = { error: "company not found" };
129+
const statusCode = 404;
130+
131+
const rejectedValue = {
132+
status: statusCode,
133+
response: {
134+
body: returnedBody
135+
}
136+
};
137+
138+
const mockRequest = sinon.stub(client, "request" as any).rejects(rejectedValue).returns(rejectedValue);
139+
const resp = await client.httpPut("/foo", { data: "bar" }, { content: "bob" });
140+
141+
expect(mockRequest).to.have.been.calledOnce;
142+
expect(resp.status).to.equal(statusCode);
143+
});
144+
145+
it("returns the correct body for successful PUT calls", async () => {
146+
const body = { ok: true };
147+
const statusCode = 200;
148+
149+
const resolvedValue = {
150+
status: statusCode,
151+
body: body
152+
};
153+
154+
const mockRequest = sinon.stub(client, "request" as any).resolves(resolvedValue);
155+
const resp = await client.httpPut("/foo", { data: "bar" }, { content: "bob" });
156+
157+
expect(mockRequest).to.have.been.calledOnce;
158+
expect(resp.error).to.be.undefined;
159+
expect(resp.body).to.deep.equal(body);
160+
expect(resp.status).to.equal(statusCode);
161+
});
162+
127163
it("returns an error response when HTTP DELETE request fails", async () => {
128164
const returnedBody = { error: "not found" };
129165
const statusCode = 404;

0 commit comments

Comments
 (0)