|
1 | 1 | import { HttpClient } from '../../src/network/HttpClient'; |
2 | 2 | import { NetworkResponse, NetworkRequestOptions } from '../../../msal-common'; |
| 3 | +import { MockedMetadataResponse } from '../utils/TestConstants'; |
3 | 4 |
|
4 | 5 | import http from "http"; |
5 | 6 | jest.mock("http", () => ({ |
@@ -199,44 +200,48 @@ describe("HttpClient", () => { |
199 | 200 | }); |
200 | 201 | }); |
201 | 202 |
|
| 203 | + |
202 | 204 | describe("Bad Status Code Error", () => { |
203 | 205 | const statusCodeError: number = 500; |
204 | | - const error: Error = new Error(`HTTP status code ${statusCodeError}`); |
| 206 | + const error: Error = new Error("Error connecting to proxy"); |
| 207 | + const networkResponse: NetworkResponse<MockedMetadataResponse> = getNetworkResponse<MockedMetadataResponse>(mockGetResponseBody, statusCodeError); |
205 | 208 |
|
206 | 209 | describe("Get Request", () => { |
207 | 210 | test("Via Https", async () => { |
208 | 211 | (https.request as jest.Mock).mockImplementationOnce(mockHttpsRequest(mockGetResponseBodyBuffer, statusCodeError)); |
209 | | - await expect(httpClient.sendGetRequestAsync(url)).rejects.toEqual(error); |
| 212 | + await expect(httpClient.sendGetRequestAsync(url)).resolves.toEqual(networkResponse); |
210 | 213 | }); |
211 | 214 |
|
212 | 215 | describe("Via Proxy", () => { |
213 | 216 | test("Http Status Code", async () => { |
214 | 217 | (http.request as jest.Mock).mockImplementationOnce(mockHttpRequest(mockGetResponseBody, statusCodeError, statusCodeError)); |
215 | | - await expect(httpClient.sendGetRequestAsync(url, getNetworkRequestOptionsWithProxyUrl)).rejects.toEqual(error); |
| 218 | + await expect(httpClient.sendGetRequestAsync(url, getNetworkRequestOptionsWithProxyUrl)).rejects.toEqual(expect.objectContaining(error)); |
216 | 219 | }); |
217 | 220 |
|
218 | 221 | test("Socket Status Code", async () => { |
219 | 222 | (http.request as jest.Mock).mockImplementationOnce(mockHttpRequest(mockGetResponseBody, statusCodeOk, statusCodeError)); |
220 | | - await expect(httpClient.sendGetRequestAsync(url, getNetworkRequestOptionsWithProxyUrl)).rejects.toEqual(error); |
| 223 | + await expect(httpClient.sendGetRequestAsync(url, getNetworkRequestOptionsWithProxyUrl)).rejects.toEqual(expect.objectContaining(error)); |
221 | 224 | }); |
222 | 225 | }); |
223 | 226 | }); |
224 | 227 |
|
225 | | - describe("Post Request", () => { |
| 228 | + describe("Post Request", <T>() => { |
| 229 | + const networkResponse: NetworkResponse<T> = getNetworkResponse(mockPostResponseBody, statusCodeError); |
| 230 | + const error: Error = new Error("Error in connection to proxy"); |
226 | 231 | test("Via Https", async () => { |
227 | 232 | (https.request as jest.Mock).mockImplementationOnce(mockHttpsRequest(mockPostResponseBodyBuffer, statusCodeError)); |
228 | | - await expect(httpClient.sendPostRequestAsync(url, postNetworkRequestOptionsWithoutProxyUrl)).rejects.toEqual(error); |
| 233 | + await expect(httpClient.sendPostRequestAsync(url, postNetworkRequestOptionsWithoutProxyUrl)).resolves.toEqual(networkResponse); |
229 | 234 | }); |
230 | 235 |
|
231 | 236 | describe("Via Proxy", () => { |
232 | 237 | test("Http Status Code", async () => { |
233 | 238 | (http.request as jest.Mock).mockImplementationOnce(mockHttpRequest(mockPostResponseBody, statusCodeError, statusCodeError)); |
234 | | - await expect(httpClient.sendPostRequestAsync(url, postNetworkRequestOptionsWithProxyUrl)).rejects.toEqual(error); |
| 239 | + await expect(httpClient.sendPostRequestAsync(url, postNetworkRequestOptionsWithProxyUrl)).rejects.toEqual(expect.objectContaining(error)); |
235 | 240 | }); |
236 | 241 |
|
237 | 242 | test("Socket Status Code", async () => { |
238 | 243 | (http.request as jest.Mock).mockImplementationOnce(mockHttpRequest(mockPostResponseBody, statusCodeOk, statusCodeError)); |
239 | | - await expect(httpClient.sendPostRequestAsync(url, postNetworkRequestOptionsWithProxyUrl)).rejects.toEqual(error); |
| 244 | + await expect(httpClient.sendPostRequestAsync(url, postNetworkRequestOptionsWithProxyUrl)).rejects.toEqual(expect.objectContaining(error)); |
240 | 245 | }); |
241 | 246 | }); |
242 | 247 | }); |
|
0 commit comments