Created
March 18, 2017 07:27
-
-
Save mjkillough/77310bba33d5bff5774a70fa2293af0d to your computer and use it in GitHub Desktop.
Hyper issue #754
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "client" | |
version = "0.1.0" | |
authors = ["Peter Sergey Panov <[email protected]>", "Michael Killough <[email protected]>"] | |
[[bin]] | |
name = "mock_client" | |
path = "main.rs" | |
[dependencies] | |
futures = "*" | |
tokio-core = "*" | |
[dependencies.hyper] | |
git = "https://github.com/hyperium/hyper" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate futures; | |
extern crate hyper; | |
extern crate tokio_core; | |
use std::io::{self, Write}; | |
use futures::Future; | |
use futures::stream::Stream; | |
use hyper::Client; | |
fn main() { | |
let mut core = tokio_core::reactor::Core::new().unwrap(); | |
let handle = core.handle(); | |
let client = Client::new(&handle); | |
let url = hyper::Url::parse("http://127.0.0.1:6667/").unwrap(); | |
let work = client.get(url).and_then(|res| { | |
println!("Response: {}", res.status()); | |
println!("Headers: \n{}", res.headers()); | |
println!("get_raw('set-cookie') -> {:?}", res.headers().get_raw("set-cookie").unwrap()); | |
println!("for hv in iter():"); | |
for hv in res.headers().iter() { | |
println!("hv.to_string() -> {}", hv.to_string()); | |
} | |
res.body().for_each(|chunk| { | |
io::stdout().write_all(&chunk).map_err(From::from) | |
}) | |
}).map(|_| { | |
println!("\n\nDone."); | |
}); | |
core.run(work).unwrap(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2.7 | |
import cherrypy | |
def multi_headers(): | |
cherrypy.response.header_list.extend( | |
cherrypy.response.headers.encode_header_items( | |
cherrypy.response.multiheaders)) | |
cherrypy.tools.multiheaders = cherrypy.Tool('on_end_resource', multi_headers) | |
TEXT = 'text/plain; charset=ISO-8859-1' | |
PORT = 6667 | |
class MockServer(object): | |
exposed = True | |
@cherrypy.expose | |
@cherrypy.tools.multiheaders() | |
def default(self, *args, **params): | |
cherrypy.response.headers['Content-Type'] = TEXT | |
if len(args) == 0: | |
cherrypy.response.status = 302 | |
cherrypy.response.multiheaders = [ | |
('Set-Cookie', "foo"), ('Set-Cookie', "bar"), ('Location', 'http://localhost:6667/ex') | |
] | |
else: | |
cherrypy.response.multiheaders = [] | |
return "" | |
if __name__ == "__main__": | |
conf = { | |
'global': { | |
'server.socket_port':PORT, | |
}, | |
} | |
staticconf = { | |
'global': { | |
'server.socket_port':PORT, | |
}, | |
} | |
cherrypy.config.update(conf) | |
cherrypy.tree.mount(MockServer(),'/',conf) | |
cherrypy.engine.start() | |
cherrypy.engine.block() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Response: 302 Found | |
Headers: | |
Content-Type: text/plain;charset=utf-8 | |
Server: CherryPy/10.2.1 | |
Date: Sat, 18 Mar 2017 07:28:20 GMT | |
Content-Length: 0 | |
Set-Cookie: foo | |
Set-Cookie: bar | |
Location: http://localhost:6667/ex | |
get_raw('set-cookie') -> [Shared([102, 111, 111]), Shared([98, 97, 114])] | |
for hv in iter(): | |
hv.to_string() -> Content-Type: text/plain;charset=utf-8 | |
hv.to_string() -> Server: CherryPy/10.2.1 | |
hv.to_string() -> Date: Sat, 18 Mar 2017 07:28:20 GMT | |
hv.to_string() -> Content-Length: 0 | |
hv.to_string() -> Set-Cookie: foo | |
Set-Cookie: bar | |
hv.to_string() -> Location: http://localhost:6667/ex | |
Done. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment