-
Notifications
You must be signed in to change notification settings - Fork 18
/
additions.test.js
140 lines (126 loc) · 4.51 KB
/
additions.test.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import isObject from "lodash/isObject.js";
import mapValues from "lodash/mapValues.js";
import axios from "axios";
import fixtures from "../../index.js";
test("reqheaders additions", async () => {
const mock = fixtures.mock("api.github.com/get-repository", {
reqheaders: {
"x-fixtures-id": "123",
},
});
try {
await axios({
method: "get",
url: "https://api.github.com/repos/octokit-fixture-org/hello-world",
headers: {
Accept: "application/vnd.github.v3+json",
Authorization: "token 0000000000000000000000000000000000000001",
},
});
throw new Error("should fail without X-Foo header");
} catch (error) {
expect(error.message).toMatch("No match for request");
}
try {
await axios({
method: "get",
url: "https://api.github.com/repos/octokit-fixture-org/hello-world",
headers: {
Accept: "application/vnd.github.v3+json",
Authorization: "token 0000000000000000000000000000000000000001",
"x-fixtures-id": "123",
},
});
} catch (error) {
throw new Error(mock.explain(error));
}
});
test("scope additions", async () => {
const mock = fixtures.mock("api.github.com/rename-repository", {
scope: "http://localhost:3000",
});
// https://developer.github.com/v3/repos/#edit
await axios({
method: "patch",
url: "http://localhost:3000/repos/octokit-fixture-org/rename-repository",
headers: {
Accept: "application/vnd.github.v3+json",
Authorization: "token 0000000000000000000000000000000000000001",
"Content-Type": "application/json; charset=utf-8",
},
data: {
name: "rename-repository-newname",
},
}).catch((error) => expect(mock.explain(error)).toBeFalsy());
// https://developer.github.com/v3/repos/#get
await axios({
method: "get",
url: "http://localhost:3000/repos/octokit-fixture-org/rename-repository",
headers: {
Accept: "application/vnd.github.v3+json",
Authorization: "token 0000000000000000000000000000000000000001",
},
maxRedirects: 0,
}).catch((error) => {
if (/No match for request/.test(error.message)) {
expect(mock.explain(error)).toBeFalsy();
}
expect(error.response.headers.location).toBe(
"http://localhost:3000/repositories/1000"
);
});
});
test("additions function", async () => {
const mapValuesDeep = (v, callback) =>
isObject(v) ? mapValues(v, (v) => mapValuesDeep(v, callback)) : callback(v);
const mock = fixtures.mock("api.github.com/release-assets", (fixture) => {
if (fixture.scope === "https://uploads.github.com:443") {
fixture.path = `/uploads.github.com${fixture.path}`;
}
fixture.scope = "http://localhost:3000";
fixture.reqheaders.host = "localhost:3000";
fixture.reqheaders["x-fixtures-id"] = "123";
return mapValuesDeep(fixture, (value) => {
if (typeof value !== "string") {
return value;
}
return value
.replace("https://api.github.com/", "http://localhost:3000/")
.replace(
"https://uploads.github.com/",
"http://localhost:3000/uploads.github.com/"
);
});
});
// https://developer.github.com/v3/repos/releases/#upload-a-release-asset
// Get release to retrieve upload URL
const { data } = await axios({
method: "get",
url: "http://localhost:3000/repos/octokit-fixture-org/release-assets/releases/tags/v1.0.0",
headers: {
Accept: "application/vnd.github.v3+json",
Authorization: "token 0000000000000000000000000000000000000001",
"x-fixtures-id": "123",
},
}).catch((error) => expect(mock.explain(error)).toBeFalsy());
expect(data.url).toBe(
"http://localhost:3000/repos/octokit-fixture-org/release-assets/releases/1000"
);
expect(data.upload_url).toBe(
"http://localhost:3000/uploads.github.com/repos/octokit-fixture-org/release-assets/releases/1000/assets{?name,label}"
);
// https://developer.github.com/v3/repos/releases/#upload-a-release-asset
// upload attachment to release URL returned by create release request
await axios({
method: "post",
url: "http://localhost:3000/uploads.github.com/repos/octokit-fixture-org/release-assets/releases/1000/assets?name=test-upload.txt&label=test",
headers: {
Accept: "application/vnd.github.v3+json",
Authorization: "token 0000000000000000000000000000000000000001",
"Content-Type": "text/plain",
"Content-Length": 14,
"x-fixtures-id": "123",
},
data: "Hello, world!\n",
}).catch((error) => console.log(mock.explain(error)));
});