-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttp_server_response.js
More file actions
152 lines (134 loc) · 4.42 KB
/
http_server_response.js
File metadata and controls
152 lines (134 loc) · 4.42 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
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
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright Titanium I.T. LLC.
import * as ensure from "util/ensure.js";
/** A response from the HTTP server to a client. */
export class HttpServerResponse {
/**
* Factory method. Creates an HTTP response.
* @param options the response details
* @param options.status status code
* @param [options.headers] headers
* @param options.body body
* @param [options.contentType] content-type header (for convenience; same as options.headers["content-type"])
* @param [options.debug] appears in logs, but is otherwise unused
* @returns {HttpServerResponse} the response
*/
static create(options) {
ensure.signature(arguments, [{
status: Number,
headers: [ undefined, Object ],
body: String,
contentType: [ undefined, String ],
debug: [ undefined, Object, String ],
}]);
const { contentType, ...remainingOptions } = options;
return new HttpServerResponse(remainingOptions, contentType);
}
/**
* Test-only factory method. Provides overridable defaults for all response details.
* @param [options] the response details
* @param options.status status code
* @param [options.headers] headers
* @param [options.body] body
* @param [options.contentType] content-type header (for convenience; same as options.headers["content-type"])
* @param [options.debug] appears in logs, but is otherwise unused
* @returns {HttpServerResponse} the response
*/
static createForTestingOnly(options = {}) {
ensure.signature(arguments, [[ undefined, {
status: [ undefined, Number ],
headers: [ undefined, Object ],
body: [ undefined, String ],
contentType: [ undefined, String ],
debug: [ undefined, Object, String ],
}]]);
const DEFAULTS = {
status: 200,
body: "default test body"
};
options = { ...DEFAULTS, ...options };
return new HttpServerResponse(options, options.contentType);
}
/**
* Convenience factory method for creating plain text responses.
* @param options the response details
* @param options.status status code
* @param [options.headers] headers
* @param options.body body
* @param [options.debug] appears in logs, but is otherwise unused
* @returns {HttpServerResponse} the response
*/
static createPlainTextResponse(options) {
ensure.signature(arguments, [{
status: Number,
headers: [ undefined, Object ],
body: String,
debug: [ undefined, Object, String ],
}]);
return new HttpServerResponse(options, "text/plain; charset=utf-8");
}
/**
* Convenience factory method for creating HTML responses.
* @param options the response details
* @param options.status status code
* @param [options.headers] headers
* @param options.body body
* @param [options.debug] appears in logs, but is otherwise unused
* @returns {HttpServerResponse} the response
*/
static createHtmlResponse(options) {
ensure.signature(arguments, [{
status: Number,
headers: [ undefined, Object ],
body: String,
debug: [ undefined, Object, String ],
}]);
return new HttpServerResponse(options, "text/html; charset=utf-8");
}
/**
* Convenience factory method for creating JSON responses.
* @param options the response details
* @param options.status status code
* @param [options.headers] headers
* @param options.body body
* @param [options.debug] appears in logs, but is otherwise unused
* @returns {HttpServerResponse} the response
*/
static createJsonResponse(options) {
ensure.signature(arguments, [{
status: Number,
headers: [ undefined, Object ],
body: Object,
debug: [ undefined, Object, String ],
}]);
options = { ...options, body: JSON.stringify(options.body) };
return new HttpServerResponse(options, "application/json");
}
/** Only for use by tests. (Use a factory method instead.) */
constructor(options, contentType) {
this._status = options.status;
const contentTypeHeaders = contentType !== undefined ? { "content-type": contentType } : {};
this._headers = { ...contentTypeHeaders, ...options.headers };
this._body = options.body;
// We provide this field so it can be viewed in failing tests. It's not used in any way.
if (options.debug instanceof Error) this._debug = options.debug.stack;
else if (options.debug !== undefined) this._debug = options.debug;
}
/**
* @returns {number} status code
*/
get status() {
return this._status;
}
/**
* @returns {object} headers
*/
get headers() {
return this._headers;
}
/**
* @returns {string} body
*/
get body() {
return this._body;
}
}