def test_url_with_query(): should_match = is_url().with_query( has_entries(key1="value1", key2="value2")) should_not_match = is_url().with_query( has_entries(key1="value1", key2="nope")) assert_that(URL, should_match) assert_that(URL, not_(should_not_match)) assert_that( should_match, has_string( "URL with query: a dictionary containing {'key1': 'value1', 'key2': 'value2'}" ), ) assert_that( should_not_match, mismatches_with(URL, "was URL with query: value for 'key2' was 'value2'")) assert_that( should_match, matches_with( URL, "was URL with query: was <{'key1': 'value1', 'key2': 'value2'}>"), )
def test_has_link(): # Given should_match = has_link(href=is_url().with_host("brunni.ng")) should_not_match_1 = has_link(href=is_url().with_host("example.com")) assert_that(HTML, should_match) assert_that(HTML, not_(should_not_match_1))
def test_response_matcher_url(): # Given response = MOCK_RESPONSE # When # Then assert_that(response, is_response().with_url(is_url().with_path("/path0"))) assert_that(response, not_(is_response().with_url(is_url().with_path("/nope")))) assert_that( str(is_response().with_url(is_url().with_path("/path0"))), contains_string("response with url: URL with path: '/path0'"), ) assert_that( is_response().with_url(is_url().with_path("/nope")), mismatches_with( response, contains_string( "was response with url: was URL with path: was </path0>")), ) assert_that( is_response().with_url(is_url().with_path("/path0")), matches_with( response, contains_string( "was response with url: was URL with path: was </path0>")), )
def test_url_with_path_segments(): should_match = is_url().with_path_segments( contains_exactly("path1", "path2", "path3")) should_not_match = is_url().with_path_segments(empty()) assert_that(URL, should_match) assert_that(URL, not_(should_not_match)) assert_that( should_match, has_string( "URL with path segments: a sequence containing ['path1', 'path2', 'path3']" ), ) assert_that( should_not_match, mismatches_with( URL, "was URL with path segments: was <['path1', 'path2', 'path3']>"), ) assert_that( should_match, matches_with( URL, "was URL with path segments: was <['path1', 'path2', 'path3']>"), )
def test_response_matcher_builder(): # Given stub_response = MOCK_RESPONSE matcher = (is_response().with_status_code(200).and_body( "sausages").and_content(b"content").and_json(has_entries( a="b")).and_headers(has_entries(key="value")).and_cookies( has_entries(name="value")).and_elapsed( between(timedelta(seconds=1), timedelta(minutes=1))).and_history( contains_exactly( is_response().with_url( is_url().with_path("/path1")), is_response().with_url( is_url().with_path("/path2")), )).and_url(is_url().with_path( "/path0")).and_encoding("utf-8")) mismatcher = is_response().with_body("kale").and_status_code(404) # When # Then assert_that(stub_response, matcher) assert_that(stub_response, not_(mismatcher)) assert_that( matcher, has_string( "response with " "status_code: <200> " "body: 'sausages' " "content: <b'content'> " "json: a dictionary containing {'a': 'b'} " "headers: a dictionary containing {'key': 'value'} " "cookies: a dictionary containing {'name': 'value'} " "elapsed: (a value greater than or equal to <0:00:01> and a value less than or equal to <0:01:00>) " "history: a sequence containing " "[response with url: URL with path: '/path1', response with url: URL with path: '/path2'] " "url: URL with path: '/path0' " "encoding: 'utf-8'"), ) assert_that( mismatcher, mismatches_with( stub_response, contains_string( "was response with status code: was <200> body: was 'sausages'" ), ), ) assert_that( matcher, matches_with( stub_response, contains_string( "was response with status code: was <200> body: was 'sausages'" ), ), )
def test_url_with_port(): should_match = is_url().with_port(1234) should_not_match = is_url().with_port(5678) assert_that(URL, should_match) assert_that(URL, not_(should_not_match)) assert_that(should_match, has_string("URL with port: <1234>")) assert_that(should_not_match, mismatches_with(URL, "was URL with port: was <1234>")) assert_that(should_match, matches_with(URL, "was URL with port: was <1234>"))
def test_url_with_host(): should_match = is_url().with_host("brunni.ng") should_not_match = is_url().with_host("example.com") assert_that(URL, should_match) assert_that(URL, not_(should_not_match)) assert_that(should_match, has_string("URL with host: 'brunni.ng'")) assert_that(should_not_match, mismatches_with(URL, "was URL with host: was 'brunni.ng'")) assert_that(should_not_match, mismatches_with(URL, "was URL with host: was 'brunni.ng'"))
def test_url_with_password(): should_match = is_url().with_password("password") should_not_match = is_url().with_password("nope") assert_that(URL, should_match) assert_that(URL, not_(should_not_match)) assert_that(should_match, has_string("URL with password: '******'")) assert_that(should_not_match, mismatches_with(URL, "was URL with password: was 'password'")) assert_that(should_match, matches_with(URL, "was URL with password: was 'password'"))
def test_url_with_username(): should_match = is_url().with_username("username") should_not_match = is_url().with_username("nope") assert_that(URL, should_match) assert_that(URL, not_(should_not_match)) assert_that(should_match, has_string("URL with username: '******'")) assert_that(should_not_match, mismatches_with(URL, "was URL with username: was 'username'")) assert_that(should_match, matches_with(URL, "was URL with username: was 'username'"))
def test_url_with_fragment(): should_match = is_url().with_fragment("fragment") should_not_match = is_url().with_fragment("banana") assert_that(URL, should_match) assert_that(URL, not_(should_not_match)) assert_that(should_match, has_string("URL with fragment: 'fragment'")) assert_that(should_not_match, mismatches_with(URL, "was URL with fragment: was <fragment>")) assert_that(should_match, matches_with(URL, "was URL with fragment: was <fragment>"))
def test_url_with_scheme(): should_match = is_url().with_scheme("https") should_not_match = is_url().and_scheme("http") assert_that(URL, should_match) assert_that(URL, not_(should_not_match)) assert_that(should_match, has_string("URL with scheme: 'https'")) assert_that(should_not_match, mismatches_with(URL, "was URL with scheme: was 'https'")) assert_that(should_match, matches_with(URL, "was URL with scheme: was 'https'"))
def test_response_history(): # Given # When actual = requests.get("https://httpbin.org/cookies/set?foo=bar") # Then assert_that( actual, is_response().with_status_code(200).and_url( is_url().with_path("/cookies")).and_history( contains_exactly(is_response().with_url( is_url().with_path("/cookies/set")))), )
def test_url_matcher_builder(): # Given should_match = (is_url().with_scheme("https").and_username( "username").and_password("password").and_host("brunni.ng").and_port( 1234).and_path("/path1/path2/path3").and_path_segments( contains_exactly("path1", "path2", "path3")).and_query( has_entries(key1="value1", key2="value2")).and_fragment("fragment")) should_not_match = is_url().with_path("woah!").and_host("example.com") assert_that(URL, should_match) assert_that(URL, not_(should_not_match)) assert_that( should_match, has_string( "URL with " "scheme: 'https' " "username: '******' " "password: '******' " "host: 'brunni.ng' " "port: <1234> " "path: '/path1/path2/path3' " "path segments: a sequence containing ['path1', 'path2', 'path3'] " "query: a dictionary containing {'key1': 'value1', 'key2': 'value2'} " "fragment: 'fragment'"), ) assert_that( should_not_match, mismatches_with( URL, "was URL with host: was 'brunni.ng' path: was </path1/path2/path3>" ), ) assert_that( should_match, matches_with( URL, "was URL with scheme: was 'https' " "username: was 'username' " "password: was 'password' " "host: was 'brunni.ng' " "port: was <1234> " "path: was </path1/path2/path3> " "path segments: was <['path1', 'path2', 'path3']> " "query: was <{'key1': 'value1', 'key2': 'value2'}> " "fragment: was <fragment>", ), )
def test_url_with_path(): should_match = is_url().with_path("/path1/path2/path3") should_not_match = is_url().with_path("/banana") assert_that(URL, should_match) assert_that(URL, not_(should_not_match)) assert_that(should_match, has_string("URL with path: '/path1/path2/path3'")) assert_that( should_not_match, mismatches_with(URL, "was URL with path: was </path1/path2/path3>")) assert_that( should_match, matches_with(URL, "was URL with path: was </path1/path2/path3>"))
def test_redirect_to(): # Given stub_response = mock.MagicMock( status_code=301, headers={"Location": a_url().with_path("/sausages").build()}) # When # Then assert_that(stub_response, redirects_to(is_url().with_path("/sausages"))) assert_that(stub_response, not_(redirects_to(is_url().with_path("/bacon")))) assert_that( redirects_to(is_url().with_path("/sausages")), has_string("redirects to URL with path: '/sausages'"), )
def test_nested_builders(): # Given builder = UrlBuilder(fragment="123") # When actual = builder.with_port(456).build() # Then assert_that(actual, instance_of(furl)) assert_that(actual, is_url().with_fragment("123"))
def test_has_row_with_link(): # Given soup = BeautifulSoup(HTML, "html.parser") table = soup.table should_match = has_row(index_matches=3, cells_match=has_item( has_link(href=is_url().with_path("/thebar")))) should_not_match_1 = has_row( index_matches=3, cells_match=has_item(has_link(href=is_url().with_path("/cup-of-tea")))) # Then assert_that(table, should_match) assert_that(table, not_(should_not_match_1)) assert_that( should_match, has_string( "table with row cells matching a sequence containing HTML with tag matching " "tag with name matching 'a' " "attributes matching (a dictionary containing ['href': URL with path: '/thebar'] and ANYTHING) " "index matching <3>"), )
def test_response_matcher_history_and_url(): # Given response = MOCK_RESPONSE # When # Then assert_that( response, is_response().with_history( contains_exactly( is_response().with_url(is_url().with_path("/path1")), is_response().with_url(is_url().with_path("/path2")), )), ) assert_that( response, not_(is_response().with_history( contains_exactly( is_response().with_url(is_url().with_path("/path1")), is_response().with_url(is_url().with_path("/path3")), ))), ) assert_that( str(is_response().with_history( contains_exactly( is_response().with_url(is_url().with_path("/path1")), is_response().with_url(is_url().with_path("/path2")), ))), contains_string( "response with history: a sequence containing " "[response with url: URL with path: '/path1', response with url: URL with path: '/path2']" ), ) assert_that( is_response().with_history( contains_exactly( is_response().with_url(is_url().with_path("/path1")), is_response().with_url(is_url().with_path("/path3")), )), mismatches_with( response, contains_string( "was response with history: item 1: was response with url: was URL with path: was </path2>" ), ), )