Skip to content

Commit 6d55b5e

Browse files
committed
allow to set HTTP status code by server in setAPIExpectations
1 parent 12a5582 commit 6d55b5e

File tree

6 files changed

+22
-18
lines changed

6 files changed

+22
-18
lines changed

test/appveyor.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ unittest
2020
j["builds"] = j["builds"][0..2];
2121
},
2222
"/appveyor/builds/greenify/dmd/13074433/cancel",
23+
HTTPStatus.noContent,
2324
(scope HTTPServerRequest req, scope HTTPServerResponse res) {
2425
assert(req.method == HTTPMethod.DELETE);
25-
res.statusCode = 204;
2626
res.writeVoidBody;
2727
},
2828
);

test/comments.d

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ unittest
186186
"/github/repos/dlang/phobos/issues/4921/labels/auto-merge",
187187
(scope HTTPServerRequest req, scope HTTPServerResponse res){
188188
assert(req.method == HTTPMethod.DELETE);
189-
res.statusCode = 200;
190189
},
191190
"/github/repos/dlang/phobos/issues/4921/comments",
192191
"/github/orgs/dlang/public_members?per_page=100",
@@ -289,7 +288,6 @@ unittest
289288
"/github/repos/dlang/phobos/issues/5519/labels/auto-merge",
290289
(scope HTTPServerRequest req, scope HTTPServerResponse res){
291290
assert(req.method == HTTPMethod.DELETE);
292-
res.statusCode = 200;
293291
},
294292
"/bugzilla/buglist.cgi?bug_id=17564&ctype=csv&columnlist=short_desc,bug_status,resolution,bug_severity,priority",
295293
"/github/repos/dlang/phobos/issues/5519/comments",

test/labels.d

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ unittest
1010
postGitHubHook("dlang_phobos_label_4921.json", "pull_request",
1111
(ref Json j, scope HTTPClientRequest req){
1212
j["pull_request"]["state"] = "open";
13-
}.toDelegate);
13+
}
14+
);
1415
}
1516

1617
@("ignore-auto-merge-on-closed")
@@ -39,20 +40,22 @@ unittest
3940
j[1]["label"]["name"] = "auto-merge";
4041
},
4142
"/github/users/9il",
42-
"/github/repos/dlang/phobos/pulls/4921/merge", (scope HTTPServerRequest req, scope HTTPServerResponse res) {
43+
"/github/repos/dlang/phobos/pulls/4921/merge",
44+
HTTPStatus.methodNotAllowed,
45+
(scope HTTPServerRequest req, scope HTTPServerResponse res) {
4346
// https://developer.github.com/v3/pulls/#response-if-merge-cannot-be-performed
4447
assert(req.json["sha"] == "d2c7d3761b73405ee39da3fd7fe5030dee35a39e");
4548
assert(req.json["merge_method"] == "merge");
4649
assert(req.json["commit_message"] == "Issue 8573 - A simpler Phobos function that returns the index of the …\n"~
4750
"merged-on-behalf-of: Ilya Yaroshenko <testmail@example.com>");
48-
res.statusCode = 405;
4951
}
5052
);
5153

5254
postGitHubHook("dlang_phobos_label_4921.json", "pull_request",
5355
(ref Json j, scope HTTPClientRequest req){
5456
j["pull_request"]["state"] = "open";
55-
}.toDelegate);
57+
}
58+
);
5659
}
5760

5861
@("succeed-to-auto-merge-squash")
@@ -74,7 +77,6 @@ unittest
7477
assert(req.json["merge_method"] == "squash");
7578
assert(req.json["commit_message"] == "Issue 8573 - A simpler Phobos function that returns the index of the …\n"~
7679
"merged-on-behalf-of: Ilya Yaroshenko <testmail@example.com>");
77-
res.statusCode = 200;
7880
}
7981
);
8082

@@ -144,7 +146,6 @@ unittest
144146
assert(req.json["merge_method"] == "merge");
145147
assert(req.json["commit_message"] == "Remove deprecated stdc import\n" ~
146148
"merged-on-behalf-of: Sebastian Wilzbach <wilzbach@users.noreply.github.com>");
147-
res.statusCode = 200;
148149
}
149150
);
150151

test/review.d

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ unittest
1313
assert(req.json["merge_method"] == "merge");
1414
assert(req.json["commit_message"] == "Fix tan returning -nan for inputs where abs(x) >= 2^63\n"~
1515
"merged-on-behalf-of: ZombineDev <ZombineDev@users.noreply.github.com>");
16-
res.statusCode = 200;
1716
}
1817
);
1918

test/status.d

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ unittest
7070
assert(req.json["merge_method"] == "merge");
7171
assert(req.json["commit_message"] == "Fix issue 16977 - bad debug info for function default arguments\n"~
7272
"merged-on-behalf-of: unknown");
73-
res.statusCode = 200;
7473
}
7574
);
7675

@@ -101,7 +100,6 @@ unittest
101100
assert(req.json["merge_method"] == "squash");
102101
assert(req.json["commit_message"] == "taking address of local means it cannot be 'scope' later\n"~
103102
"merged-on-behalf-of: Martin Nowak <somemail@example.org>");
104-
res.statusCode = 200;
105103
}
106104
);
107105

@@ -130,7 +128,6 @@ unittest
130128
assert(req.json["merge_method"] == "merge");
131129
assert(req.json["commit_message"] == "Fix issue 16977 - bad debug info for function default arguments\n"~
132130
"merged-on-behalf-of: unknown");
133-
res.statusCode = 200;
134131
},
135132
"/github/repos/dlang/dmd/pulls/6328/commits",
136133
"/github/repos/dlang/dmd/issues/6328/events",
@@ -141,7 +138,6 @@ unittest
141138
assert(req.json["merge_method"] == "squash");
142139
assert(req.json["commit_message"] == "taking address of local means it cannot be 'scope' later\n"~
143140
"merged-on-behalf-of: Martin Nowak <somemail@example.org>");
144-
res.statusCode = 200;
145141
}
146142
);
147143

test/utils.d

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import std.functional, std.string;
55

66
// forward commonly needed imports
77
public import dlangbot.app;
8-
public import vibe.http.common : HTTPMethod;
8+
public import vibe.http.common : HTTPMethod, HTTPStatus;
99
public import vibe.http.client : HTTPClientRequest;
1010
public import vibe.http.server : HTTPServerRequest, HTTPServerResponse;
1111
public import std.functional : toDelegate;
@@ -104,6 +104,8 @@ auto payloadServer(scope HTTPServerRequest req, scope HTTPServerResponse res)
104104
assert(0, "Request for unexpected URL received: " ~ req.requestURL);
105105
}
106106

107+
res.statusCode = expectation.respStatusCode;
108+
107109
string filePath = buildPath(payloadDir, req.requestURL[1 .. $].replace("/", "_"));
108110

109111
if (expectation.reqHandler !is null)
@@ -169,14 +171,18 @@ void replaceAPIReferences(string official, string local, ref Json json)
169171

170172
struct APIExpectation
171173
{
174+
/// the called server url
172175
string url;
173176

174-
// implement a custom request handler
177+
/// implement a custom request handler
175178
private void delegate(scope HTTPServerRequest req, scope HTTPServerResponse res) reqHandler;
176179

177-
// modify the json of the payload before being served
180+
/// modify the json of the payload before being served
178181
private void delegate(ref Json j) jsonHandler;
179182

183+
/// respond with the given status
184+
HTTPStatus respStatusCode = HTTPStatus.ok;
185+
180186
this(string url)
181187
{
182188
this.url = url;
@@ -195,7 +201,11 @@ void setAPIExpectations(Args...)(Args args)
195201
{
196202
static if (is(Args[i] : string))
197203
{
198-
apiExpectations ~= APIExpectation(arg);
204+
apiExpectations ~= APIExpectation(arg);
205+
}
206+
else static if (is(Args[i] : HTTPStatus))
207+
{
208+
apiExpectations[$ - 1].respStatusCode = arg;
199209
}
200210
else
201211
{

0 commit comments

Comments
 (0)