deno.land / x / opine@2.3.4 / test / units / req.hostname.test.ts

req.hostname.test.ts
نووسراو ببینە
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { opine } from "../../mod.ts";import { superdeno } from "../deps.ts";import { describe, it } from "../utils.ts";
// Custom Host headers removed in Deno 1.12.0 meaning the majority// of these tests cannot be used.//// REF: https://github.com/denoland/deno/issues/11017
describe("req", function () { describe(".hostname", function () { it("should return the Host", function (done) { const app = opine();
app.use(function (req, res) { res.end(req.hostname); });
superdeno(app) .post("/") .expect("127.0.0.1", done); });
// it("should return the Host when present", function (done) { // const app = opine();
// app.use(function (req, res) { // res.end(req.hostname); // });
// superdeno(app) // .post("/") // .set("Host", "example.com") // .expect("example.com", done); // });
// it("should strip port number", function (done) { // const app = opine();
// app.use(function (req, res) { // res.end(req.hostname); // });
// superdeno(app) // .post("/") // .set("Host", "example.com:3000") // .expect("example.com", done); // });
it("should return undefined otherwise", function (done) { const app = opine();
app.use(function (req, res) { req.headers.delete("host"); res.end(String(req.hostname)); });
superdeno(app) .post("/") .expect("undefined", done); });
// it("should work with IPv6 Host", function (done) { // const app = opine();
// app.use(function (req, res) { // res.end(req.hostname); // });
// superdeno(app) // .post("/") // .set("Host", "[::1]") // .expect("[::1]", done); // });
// it("should work with IPv6 Host and port", function (done) { // const app = opine();
// app.use(function (req, res) { // res.end(req.hostname); // });
// superdeno(app) // .post("/") // .set("Host", "[::1]:3000") // .expect("[::1]", done); // });
describe('when "trust proxy" is enabled', function () { it("should respect X-Forwarded-Host", function (done) { const app = opine();
app.enable("trust proxy");
app.use(function (req, res) { res.end(req.hostname); });
superdeno(app) .get("/") .set("X-Forwarded-Host", "example.com:3000") .expect("example.com", done); });
it("should ignore X-Forwarded-Host if socket addr not trusted", function ( done, ) { const app = opine();
app.set("trust proxy", "10.0.0.1");
app.use(function (req, res) { res.end(req.hostname); });
superdeno(app) .get("/") .set("X-Forwarded-Host", "example.com") .expect("127.0.0.1", done); });
// it("should default to Host", function (done) { // const app = opine();
// app.enable("trust proxy");
// app.use(function (req, res) { // res.end(req.hostname); // });
// superdeno(app) // .get("/") // .set("Host", "example.com") // .expect("example.com", done); // });
describe("when multiple X-Forwarded-Host", function () { it("should use the first value", function (done) { const app = opine();
app.enable("trust proxy");
app.use(function (req, res) { res.send(req.hostname); });
superdeno(app) .get("/") .set("X-Forwarded-Host", "example.com, foobar.com") .expect(200, "example.com", done); });
it("should remove OWS around comma", function (done) { const app = opine();
app.enable("trust proxy");
app.use(function (req, res) { res.send(req.hostname); });
superdeno(app) .get("/") .set("X-Forwarded-Host", "example.com , foobar.com") .expect(200, "example.com", done); });
it("should strip port number", function (done) { const app = opine();
app.enable("trust proxy");
app.use(function (req, res) { res.send(req.hostname); });
superdeno(app) .get("/") .set("X-Forwarded-Host", "example.com:8080 , foobar.com:8888") .expect(200, "example.com", done); }); }); });
describe('when "trust proxy" is disabled', function () { it("should ignore X-Forwarded-Host", function (done) { const app = opine();
app.use(function (req, res) { res.end(req.hostname); });
superdeno(app) .get("/") .set("X-Forwarded-Host", "evil") .expect("127.0.0.1", done); }); }); });});
opine

Version Info

Tagged at
2 years ago