mirror of
https://github.com/denoland/deno.git
synced 2025-03-04 01:44:26 -05:00
ops/fetch: add statusText (#2851)
This commit is contained in:
parent
0ce15f08c7
commit
56508f113d
3 changed files with 9 additions and 3 deletions
|
@ -50,7 +50,7 @@ pub fn op_fetch(
|
||||||
}
|
}
|
||||||
debug!("Before fetch {}", url);
|
debug!("Before fetch {}", url);
|
||||||
let future = request.send().map_err(ErrBox::from).and_then(move |res| {
|
let future = request.send().map_err(ErrBox::from).and_then(move |res| {
|
||||||
let status = res.status().as_u16();
|
let status = res.status();
|
||||||
let mut res_headers = Vec::new();
|
let mut res_headers = Vec::new();
|
||||||
for (key, val) in res.headers().iter() {
|
for (key, val) in res.headers().iter() {
|
||||||
res_headers.push((key.to_string(), val.to_str().unwrap().to_owned()));
|
res_headers.push((key.to_string(), val.to_str().unwrap().to_owned()));
|
||||||
|
@ -61,7 +61,8 @@ pub fn op_fetch(
|
||||||
|
|
||||||
let json_res = json!({
|
let json_res = json!({
|
||||||
"bodyRid": body_resource.rid,
|
"bodyRid": body_resource.rid,
|
||||||
"status": status,
|
"status": status.as_u16(),
|
||||||
|
"statusText": status.canonical_reason().unwrap_or(""),
|
||||||
"headers": res_headers
|
"headers": res_headers
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -243,7 +243,6 @@ class Body implements domTypes.Body, domTypes.ReadableStream, io.ReadCloser {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Response implements domTypes.Response {
|
export class Response implements domTypes.Response {
|
||||||
statusText = "FIXME"; // TODO
|
|
||||||
readonly type = "basic"; // TODO
|
readonly type = "basic"; // TODO
|
||||||
readonly redirected: boolean;
|
readonly redirected: boolean;
|
||||||
headers: domTypes.Headers;
|
headers: domTypes.Headers;
|
||||||
|
@ -254,6 +253,7 @@ export class Response implements domTypes.Response {
|
||||||
constructor(
|
constructor(
|
||||||
readonly url: string,
|
readonly url: string,
|
||||||
readonly status: number,
|
readonly status: number,
|
||||||
|
readonly statusText: string,
|
||||||
headersList: Array<[string, string]>,
|
headersList: Array<[string, string]>,
|
||||||
rid: number,
|
rid: number,
|
||||||
redirected_: boolean,
|
redirected_: boolean,
|
||||||
|
@ -313,6 +313,7 @@ export class Response implements domTypes.Response {
|
||||||
return new Response(
|
return new Response(
|
||||||
this.url,
|
this.url,
|
||||||
this.status,
|
this.status,
|
||||||
|
this.statusText,
|
||||||
headersList,
|
headersList,
|
||||||
-1,
|
-1,
|
||||||
this.redirected,
|
this.redirected,
|
||||||
|
@ -324,6 +325,7 @@ export class Response implements domTypes.Response {
|
||||||
interface FetchResponse {
|
interface FetchResponse {
|
||||||
bodyRid: number;
|
bodyRid: number;
|
||||||
status: number;
|
status: number;
|
||||||
|
statusText: string;
|
||||||
headers: Array<[string, string]>;
|
headers: Array<[string, string]>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -422,6 +424,7 @@ export async function fetch(
|
||||||
const response = new Response(
|
const response = new Response(
|
||||||
url,
|
url,
|
||||||
fetchResponse.status,
|
fetchResponse.status,
|
||||||
|
fetchResponse.statusText,
|
||||||
fetchResponse.headers,
|
fetchResponse.headers,
|
||||||
fetchResponse.bodyRid,
|
fetchResponse.bodyRid,
|
||||||
redirected
|
redirected
|
||||||
|
|
|
@ -107,6 +107,7 @@ testPerm(
|
||||||
testPerm({ net: true }, async function fetchWithRedirection(): Promise<void> {
|
testPerm({ net: true }, async function fetchWithRedirection(): Promise<void> {
|
||||||
const response = await fetch("http://localhost:4546/"); // will redirect to http://localhost:4545/
|
const response = await fetch("http://localhost:4546/"); // will redirect to http://localhost:4545/
|
||||||
assertEquals(response.status, 200);
|
assertEquals(response.status, 200);
|
||||||
|
assertEquals(response.statusText, "OK");
|
||||||
assertEquals(response.url, "http://localhost:4545/");
|
assertEquals(response.url, "http://localhost:4545/");
|
||||||
const body = await response.text();
|
const body = await response.text();
|
||||||
assert(body.includes("<title>Directory listing for /</title>"));
|
assert(body.includes("<title>Directory listing for /</title>"));
|
||||||
|
@ -117,6 +118,7 @@ testPerm({ net: true }, async function fetchWithRelativeRedirection(): Promise<
|
||||||
> {
|
> {
|
||||||
const response = await fetch("http://localhost:4545/tests"); // will redirect to /tests/
|
const response = await fetch("http://localhost:4545/tests"); // will redirect to /tests/
|
||||||
assertEquals(response.status, 200);
|
assertEquals(response.status, 200);
|
||||||
|
assertEquals(response.statusText, "OK");
|
||||||
const body = await response.text();
|
const body = await response.text();
|
||||||
assert(body.includes("<title>Directory listing for /tests/</title>"));
|
assert(body.includes("<title>Directory listing for /tests/</title>"));
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue