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
extern crate serde; extern crate tokio; use self::serde::Serialize; use self::tokio::runtime::current_thread::Runtime; use actix_web::client::{ClientRequestBuilder, ClientResponse}; use actix_web::http::Cookie; use actix_web::Body; use assert::*; use expect::Expect; use expect_builder::GabiraExpectBuilder; use std::borrow::Cow; use std::boxed::Box; pub struct GabiraRequestBuilder { pub req_builder: ClientRequestBuilder, } impl<'a> GabiraRequestBuilder { #[must_use = "request builder does nothing unless consumed by an expectation builder"] pub fn set_header(mut self, field: &str, value: &str) -> GabiraRequestBuilder { self.req_builder.set_header(field, value); self } #[must_use = "request builder does nothing unless consumed by an expectation builder"] pub fn set_cookie(mut self, name: &str, value: &str) -> GabiraRequestBuilder { self.req_builder.cookie(Cookie::new( Cow::Borrowed(name).into_owned(), Cow::Borrowed(value).into_owned(), )); self } #[must_use = "expectation builder does nothing unless consumed"] pub fn send<B: Into<Body>>(mut self, body: B) -> GabiraExpectBuilder<'a> { GabiraExpectBuilder { req: self.req_builder.body(body).unwrap(), expectations: vec![], runtime: Runtime::new().unwrap(), } } #[must_use = "expectation builder does nothing unless consumed"] pub fn send_json<T: Serialize>(mut self, json: T) -> GabiraExpectBuilder<'a> { GabiraExpectBuilder { req: self.req_builder.json(json).unwrap(), expectations: vec![], runtime: Runtime::new().unwrap(), } } #[must_use = "expectation builder does nothing unless consumed"] pub fn send_form<T: Serialize>(mut self, form: T) -> GabiraExpectBuilder<'a> { GabiraExpectBuilder { req: self.req_builder.form(form).unwrap(), expectations: vec![], runtime: Runtime::new().unwrap(), } } } impl<'a> Expect<'a> for GabiraRequestBuilder { fn expect<F>(mut self, f: F) -> GabiraExpectBuilder<'a> where F: FnMut(&ClientResponse) + 'a, { GabiraExpectBuilder { req: self.req_builder.finish().unwrap(), expectations: vec![Box::new(f)], runtime: Runtime::new().unwrap(), } } fn expect_header(mut self, field: &'a str, value: &'a str) -> GabiraExpectBuilder<'a> { GabiraExpectBuilder { req: self.req_builder.finish().unwrap(), expectations: vec![create_header_assert(field, value)], runtime: Runtime::new().unwrap(), } } fn expect_status(mut self, status: u16) -> GabiraExpectBuilder<'a> { GabiraExpectBuilder { req: self.req_builder.finish().unwrap(), expectations: vec![create_status_assert(status)], runtime: Runtime::new().unwrap(), } } fn expect_body<B: Into<Body> + 'a>(mut self, body: B) -> GabiraExpectBuilder<'a> { GabiraExpectBuilder { req: self.req_builder.finish().unwrap(), expectations: vec![create_body_assert(body.into())], runtime: Runtime::new().unwrap(), } } fn expect_json<T: Serialize>(mut self, json: T) -> GabiraExpectBuilder<'a> { GabiraExpectBuilder { req: self.req_builder.finish().unwrap(), expectations: vec![ create_header_assert("Content-Type", "application/json"), create_json_assert(json), ], runtime: Runtime::new().unwrap(), } } fn expect_form<T: Serialize>(mut self, form: T) -> GabiraExpectBuilder<'a> { GabiraExpectBuilder { req: self.req_builder.finish().unwrap(), expectations: vec![ create_header_assert("Content-Type", "application/x-www-form-urlencoded"), create_form_assert(form), ], runtime: Runtime::new().unwrap(), } } fn expect_cookie(mut self, name: &'a str, value: &'a str) -> GabiraExpectBuilder<'a> { GabiraExpectBuilder { req: self.req_builder.finish().unwrap(), expectations: vec![create_cookie_assert(name, value)], runtime: Runtime::new().unwrap(), } } }