mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 12:49:10 +00:00
feat: Set user agent for http client (#2916)
This commit is contained in:
parent
a4e1d7d2e7
commit
85c51404ae
3 changed files with 22 additions and 0 deletions
|
@ -1,11 +1,14 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
use crate::deno_error;
|
use crate::deno_error;
|
||||||
use crate::deno_error::DenoError;
|
use crate::deno_error::DenoError;
|
||||||
|
use crate::version;
|
||||||
use deno::ErrBox;
|
use deno::ErrBox;
|
||||||
use futures::{future, Future};
|
use futures::{future, Future};
|
||||||
use reqwest;
|
use reqwest;
|
||||||
|
use reqwest::header::HeaderMap;
|
||||||
use reqwest::header::CONTENT_TYPE;
|
use reqwest::header::CONTENT_TYPE;
|
||||||
use reqwest::header::LOCATION;
|
use reqwest::header::LOCATION;
|
||||||
|
use reqwest::header::USER_AGENT;
|
||||||
use reqwest::r#async::Client;
|
use reqwest::r#async::Client;
|
||||||
use reqwest::RedirectPolicy;
|
use reqwest::RedirectPolicy;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
@ -13,8 +16,14 @@ use url::Url;
|
||||||
/// Create new instance of async reqwest::Client. This client supports
|
/// Create new instance of async reqwest::Client. This client supports
|
||||||
/// proxies and doesn't follow redirects.
|
/// proxies and doesn't follow redirects.
|
||||||
pub fn get_client() -> Client {
|
pub fn get_client() -> Client {
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert(
|
||||||
|
USER_AGENT,
|
||||||
|
format!("Deno/{}", version::DENO).parse().unwrap(),
|
||||||
|
);
|
||||||
Client::builder()
|
Client::builder()
|
||||||
.redirect(RedirectPolicy::none())
|
.redirect(RedirectPolicy::none())
|
||||||
|
.default_headers(headers)
|
||||||
.use_sys_proxy()
|
.use_sys_proxy()
|
||||||
.build()
|
.build()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|
|
@ -220,6 +220,16 @@ testPerm({ net: true }, async function fetchInitBlobBody(): Promise<void> {
|
||||||
assert(response.headers.get("content-type").startsWith("text/javascript"));
|
assert(response.headers.get("content-type").startsWith("text/javascript"));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testPerm({ net: true }, async function fetchUserAgent(): Promise<void> {
|
||||||
|
const data = "Hello World";
|
||||||
|
const response = await fetch("http://localhost:4545/echo_server", {
|
||||||
|
method: "POST",
|
||||||
|
body: new TextEncoder().encode(data)
|
||||||
|
});
|
||||||
|
assertEquals(response.headers.get("user-agent"), `Deno/${Deno.version.deno}`);
|
||||||
|
await response.text();
|
||||||
|
});
|
||||||
|
|
||||||
// TODO(ry) The following tests work but are flaky. There's a race condition
|
// TODO(ry) The following tests work but are flaky. There's a race condition
|
||||||
// somewhere. Here is what one of these flaky failures looks like:
|
// somewhere. Here is what one of these flaky failures looks like:
|
||||||
//
|
//
|
||||||
|
|
|
@ -61,6 +61,9 @@ class ContentTypeHandler(QuietSimpleHTTPRequestHandler):
|
||||||
if self.headers.has_key('content-type'):
|
if self.headers.has_key('content-type'):
|
||||||
self.send_header('content-type',
|
self.send_header('content-type',
|
||||||
self.headers.getheader('content-type'))
|
self.headers.getheader('content-type'))
|
||||||
|
if self.headers.has_key('user-agent'):
|
||||||
|
self.send_header('user-agent',
|
||||||
|
self.headers.getheader('user-agent'))
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
data_string = self.rfile.read(int(self.headers['Content-Length']))
|
data_string = self.rfile.read(int(self.headers['Content-Length']))
|
||||||
self.wfile.write(bytes(data_string))
|
self.wfile.write(bytes(data_string))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue