1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 00:59:24 -05:00

fix(http): remove unwrap() in HTTP bindings (#11130)

This commit is contained in:
Bartek Iwańczuk 2021-06-28 00:19:40 +02:00 committed by GitHub
parent 098a7c8886
commit 9e875b2a23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View file

@ -586,3 +586,48 @@ unitTest({ perms: { net: true } }, async function httpRequestLatin1Headers() {
await promise;
});
unitTest(
{ perms: { net: true } },
async function httpServerRequestWithoutPath() {
const promise = (async () => {
const listener = Deno.listen({ port: 4501 });
for await (const conn of listener) {
const httpConn = Deno.serveHttp(conn);
for await (const { request, respondWith } of httpConn) {
assertEquals(new URL(request.url).href, "http://127.0.0.1/");
assertEquals(await request.text(), "");
respondWith(new Response());
}
break;
}
})();
const clientConn = await Deno.connect({ port: 4501 });
async function writeRequest(conn: Deno.Conn) {
const encoder = new TextEncoder();
const w = new BufWriter(conn);
const r = new BufReader(conn);
const body =
`CONNECT 127.0.0.1:4501 HTTP/1.1\r\nHost: 127.0.0.1:4501\r\n\r\n`;
const writeResult = await w.write(encoder.encode(body));
assertEquals(body.length, writeResult);
await w.flush();
const tpr = new TextProtoReader(r);
const statusLine = await tpr.readLine();
assert(statusLine !== null);
const m = statusLine.match(/^(.+?) (.+?) (.+?)$/);
assert(m !== null, "must be matched");
const [_, _proto, status, _ok] = m;
assertEquals(status, "200");
const headers = await tpr.readMIMEHeader();
assert(headers !== null);
}
await writeRequest(clientConn);
clientConn.close();
await promise;
},
);

View file

@ -222,7 +222,7 @@ async fn op_http_request_next(
} else {
Cow::Owned(conn_resource.addr.to_string())
};
let path = req.uri().path_and_query().unwrap();
let path = req.uri().path_and_query().map_or("/", |p| p.as_str());
format!("{}://{}{}", scheme, host, path)
};