Added contextData to request.
Build and Test Module / build-and-test (push) Successful in 6s
Details
Build and Test Module / build-and-test (push) Successful in 6s
Details
This commit is contained in:
parent
bb1d04cfa3
commit
8b198c58ee
|
@ -22,6 +22,7 @@ struct ServerHttpRequestBuilder {
|
||||||
string[][string] headers;
|
string[][string] headers;
|
||||||
QueryParameter[] queryParams;
|
QueryParameter[] queryParams;
|
||||||
InputStream!ubyte inputStream = inputStreamObjectFor(NoOpInputStream!ubyte());
|
InputStream!ubyte inputStream = inputStreamObjectFor(NoOpInputStream!ubyte());
|
||||||
|
Object[string] contextData;
|
||||||
|
|
||||||
ref withVersion(HttpVersion httpVersion) {
|
ref withVersion(HttpVersion httpVersion) {
|
||||||
this.httpVersion = httpVersion;
|
this.httpVersion = httpVersion;
|
||||||
|
@ -75,6 +76,11 @@ struct ServerHttpRequestBuilder {
|
||||||
return withBody(cast(ubyte[]) bodyStr);
|
return withBody(cast(ubyte[]) bodyStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ref withContextData(string key, Object obj) {
|
||||||
|
this.contextData[key] = obj;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
ServerHttpRequest build() {
|
ServerHttpRequest build() {
|
||||||
return ServerHttpRequest(
|
return ServerHttpRequest(
|
||||||
httpVersion,
|
httpVersion,
|
||||||
|
@ -83,12 +89,20 @@ struct ServerHttpRequestBuilder {
|
||||||
url,
|
url,
|
||||||
headers,
|
headers,
|
||||||
queryParams,
|
queryParams,
|
||||||
inputStream
|
inputStream,
|
||||||
|
contextData
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unittest {
|
unittest {
|
||||||
|
class SampleContextData {
|
||||||
|
string name;
|
||||||
|
this(string name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ServerHttpRequest r1 = ServerHttpRequestBuilder()
|
ServerHttpRequest r1 = ServerHttpRequestBuilder()
|
||||||
.withUrl("/test-url")
|
.withUrl("/test-url")
|
||||||
.withVersion(HttpVersion.V2)
|
.withVersion(HttpVersion.V2)
|
||||||
|
@ -98,6 +112,7 @@ unittest {
|
||||||
.withHeader("Content-Type", "text/plain")
|
.withHeader("Content-Type", "text/plain")
|
||||||
.withHeader("Content-Length", "12")
|
.withHeader("Content-Length", "12")
|
||||||
.withQueryParam("idx", "42")
|
.withQueryParam("idx", "42")
|
||||||
|
.withContextData("name", new SampleContextData("andrew"))
|
||||||
.build();
|
.build();
|
||||||
assert(r1.httpVersion == HttpVersion.V2);
|
assert(r1.httpVersion == HttpVersion.V2);
|
||||||
assert(r1.url == "/test-url");
|
assert(r1.url == "/test-url");
|
||||||
|
@ -109,6 +124,7 @@ unittest {
|
||||||
assert(r1.getHeaderAs!string("Content-Type") == "text/plain");
|
assert(r1.getHeaderAs!string("Content-Type") == "text/plain");
|
||||||
assert(r1.getHeaderAs!ulong("Content-Length") == 12);
|
assert(r1.getHeaderAs!ulong("Content-Length") == 12);
|
||||||
assert(r1.getParamAs!ulong("idx") == 42);
|
assert(r1.getParamAs!ulong("idx") == 42);
|
||||||
|
assert((cast(SampleContextData) r1.contextData["name"]).name == "andrew");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -29,6 +29,8 @@ struct ServerHttpRequest {
|
||||||
const QueryParameter[] queryParams;
|
const QueryParameter[] queryParams;
|
||||||
/// The underlying stream used to read the body from the request.
|
/// The underlying stream used to read the body from the request.
|
||||||
InputStream!ubyte inputStream;
|
InputStream!ubyte inputStream;
|
||||||
|
/// Any additional data about this request that may be populated during handling.
|
||||||
|
Object[string] contextData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a header as the specified type, or returns the default value if the
|
* Gets a header as the specified type, or returns the default value if the
|
||||||
|
|
Loading…
Reference in New Issue