Skip to content

Commit 2936bdf

Browse files
Leverage template strings + expectAsync/toBeRejected based on PR feedback
1 parent 55101b3 commit 2936bdf

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

spec/HTTPRequest.spec.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const httpRequest = require('../lib/cloud-code/httpRequest'),
66
express = require('express');
77

88
const port = 13371;
9-
const httpRequestServer = 'http://localhost:' + port;
9+
const httpRequestServer = `http://localhost:${port}`;
1010

1111
function startServer(done) {
1212
const app = express();
@@ -53,7 +53,7 @@ describe('httpRequest', () => {
5353

5454
it('should do /hello', async () => {
5555
const httpResponse = await httpRequest({
56-
url: httpRequestServer + '/hello',
56+
url: `${httpRequestServer}/hello`,
5757
});
5858

5959
expect(httpResponse.status).toBe(200);
@@ -64,15 +64,15 @@ describe('httpRequest', () => {
6464

6565
it('should do not follow redirects by default', async () => {
6666
const httpResponse = await httpRequest({
67-
url: httpRequestServer + '/301',
67+
url: `${httpRequestServer}/301`,
6868
});
6969

7070
expect(httpResponse.status).toBe(301);
7171
});
7272

7373
it('should follow redirects when set', async () => {
7474
const httpResponse = await httpRequest({
75-
url: httpRequestServer + '/301',
75+
url: `${httpRequestServer}/301`,
7676
followRedirects: true,
7777
});
7878

@@ -83,24 +83,24 @@ describe('httpRequest', () => {
8383
});
8484

8585
it('should fail on 404', async () => {
86-
try {
87-
await httpRequest({
88-
url: httpRequestServer + '/404',
89-
});
90-
91-
fail('should not succeed');
92-
} catch (httpResponse) {
93-
expect(httpResponse.status).toBe(404);
94-
expect(httpResponse.buffer).toEqual(Buffer.from('NO'));
95-
expect(httpResponse.text).toEqual('NO');
96-
expect(httpResponse.data).toBe(undefined);
97-
}
86+
await expectAsync(
87+
httpRequest({
88+
url: `${httpRequestServer}/404`,
89+
})
90+
).toBeRejectedWith(
91+
jasmine.objectContaining({
92+
status: 404,
93+
buffer: Buffer.from('NO'),
94+
text: 'NO',
95+
data: undefined,
96+
})
97+
);
9898
});
9999

100100
it('should post on echo', async () => {
101101
const httpResponse = await httpRequest({
102102
method: 'POST',
103-
url: httpRequestServer + '/echo',
103+
url: `${httpRequestServer}/echo`,
104104
body: {
105105
foo: 'bar',
106106
},
@@ -154,21 +154,16 @@ describe('httpRequest', () => {
154154
});
155155

156156
it('should fail gracefully', async () => {
157-
try {
158-
await httpRequest({
157+
await expectAsync(
158+
httpRequest({
159159
url: 'http://not a good url',
160-
});
161-
162-
fail('should not succeed');
163-
} catch (error) {
164-
expect(error).not.toBeUndefined();
165-
expect(error).not.toBeNull();
166-
}
160+
})
161+
).toBeRejected();
167162
});
168163

169164
it('should params object to query string', async () => {
170165
const httpResponse = await httpRequest({
171-
url: httpRequestServer + '/qs',
166+
url: `${httpRequestServer}/qs`,
172167
params: {
173168
foo: 'bar',
174169
},
@@ -180,7 +175,7 @@ describe('httpRequest', () => {
180175

181176
it('should params string to query string', async () => {
182177
const httpResponse = await httpRequest({
183-
url: httpRequestServer + '/qs',
178+
url: `${httpRequestServer}/qs`,
184179
params: 'foo=bar&foo2=bar2',
185180
});
186181

@@ -204,6 +199,7 @@ describe('httpRequest', () => {
204199

205200
const serialized = JSON.stringify(httpResponse);
206201
const result = JSON.parse(serialized);
202+
207203
expect(result.text).toBe('hello');
208204
expect(result.data).toBe(undefined);
209205
expect(result.body).toBe(undefined);
@@ -231,6 +227,7 @@ describe('httpRequest', () => {
231227

232228
const serialized = JSON.stringify(httpResponse);
233229
const result = JSON.parse(serialized);
230+
234231
expect(result.text).toBe('hello');
235232
expect(result.data).toBe(undefined);
236233
});
@@ -240,6 +237,7 @@ describe('httpRequest', () => {
240237
const httpResponse = new HTTPResponse({}, Buffer.from(json));
241238
const serialized = JSON.stringify(httpResponse);
242239
const result = JSON.parse(serialized);
240+
243241
expect(result.text).toEqual('{"foo":"bar"}');
244242
expect(result.data).toEqual({ foo: 'bar' });
245243
});
@@ -251,17 +249,19 @@ describe('httpRequest', () => {
251249
let foundData,
252250
foundText,
253251
foundBody = false;
252+
254253
for (const key in encoded) {
255-
if (key == 'data') {
254+
if (key === 'data') {
256255
foundData = true;
257256
}
258-
if (key == 'text') {
257+
if (key === 'text') {
259258
foundText = true;
260259
}
261-
if (key == 'body') {
260+
if (key === 'body') {
262261
foundBody = true;
263262
}
264263
}
264+
265265
expect(foundData).toBe(true);
266266
expect(foundText).toBe(true);
267267
expect(foundBody).toBe(false);

0 commit comments

Comments
 (0)