-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
112 lines (101 loc) · 3.1 KB
/
test.js
File metadata and controls
112 lines (101 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const test = require('ava');
const micro = require('micro');
const listen = require('test-listen');
const rp = require('request-promise');
const pkg = require('./package.json');
const makeService = require('.');
const MockBackend = ({input}) =>
Promise.resolve({name: input.name, body: input.body, message: 'testing'});
/**
* Thin wrapper around request-promise
* that always resolves to a full response
* object rather than just the response body
* and automatically parses JSON respones.
*
* @param {object} options: Options passed to request-promise.
* @returns {Promise} The response.
*/
const request = options => {
const defaults = {
resolveWithFullResponse: true,
json: true,
};
const opts = {...defaults, ...options};
return rp(opts);
};
/**
* Make a mocked service.
*
* @param {object} backend: MicroFeedback backend.
* @returns {object} service and service URL.
*/
const maketestService = async backend => {
const handler = makeService(backend || MockBackend, {
name: 'test-backend',
version: '0.1.0',
});
const service = micro(handler);
const url = await listen(service);
return {service, url};
};
test('GET: success', async t => {
const {url} = await maketestService();
const {body, statusCode} = await request({uri: url});
t.is(statusCode, 200);
t.is(body.core.version, pkg.version);
});
test('GET: returns backend attributes', async t => {
const {url} = await maketestService();
const {body} = await request({uri: url});
t.deepEqual(body.backend, {name: 'test-backend', version: '0.1.0'});
});
test('POST: success', async t => {
const {url} = await maketestService();
const body = {name: 'steve', body: 'wat'};
const response = await request({uri: url, method: 'POST', body});
t.is(response.statusCode, 201);
});
test('POST: response format', async t => {
const {url} = await maketestService();
const payload = {name: 'steve', body: 'wat'};
const {body} = await request({uri: url, method: 'POST', body: payload});
t.deepEqual(body.result, {name: 'steve', body: 'wat', message: 'testing'});
t.deepEqual(body.backend, {name: 'test-backend', version: '0.1.0'});
});
test('POST: no issue body given', async t => {
const {url} = await maketestService();
const body = {name: 'steve'};
const response = await request({
uri: url,
method: 'POST',
body,
simple: false,
});
t.is(response.statusCode, 422);
});
test('POST: error thrown by backend', async t => {
const ErrorBackend = () => {
throw micro.createError(400, 'Error thrown by backend');
};
const {url} = await maketestService(ErrorBackend);
const payload = {name: 'steve', body: 'wat'};
const {body, statusCode} = await request({
uri: url,
method: 'POST',
body: payload,
simple: false,
});
t.is(statusCode, 400);
t.deepEqual(body, {status: 400, message: 'Error thrown by backend'});
});
test('PUT not supported', async t => {
const {url} = await maketestService();
const body = {name: 'steve', body: 'wat'};
const response = await request({
uri: url,
method: 'PUT',
body,
simple: false,
});
t.is(response.statusCode, 405);
});