def test_taxii_endpoint_raises_exception(): """Test exception is raised when conn and (user or pass) is provided""" conn = _HTTPConnection(user="******", password="******", verify=False) error_str = "Only one of a connection, username/password, or auth object may be provided." fake_url = "https://example.com/api1/collections/" with pytest.raises(InvalidArgumentsError) as excinfo: _TAXIIEndpoint(fake_url, conn, "other", "test") assert error_str in str(excinfo.value) with pytest.raises(InvalidArgumentsError) as excinfo: _TAXIIEndpoint(fake_url, conn, auth=TokenAuth('abcd')) assert error_str in str(excinfo.value) with pytest.raises(InvalidArgumentsError) as excinfo: _TAXIIEndpoint(fake_url, user="******", password="******", auth=TokenAuth('abcd')) assert error_str in str(excinfo.value) with pytest.raises(InvalidArgumentsError) as excinfo: _TAXIIEndpoint(fake_url, conn, "other", "test", auth=TokenAuth('abcd')) assert error_str in str(excinfo.value)
def init_server(self, version=TAXII_VER_2_0): """ Initializes a server in the requested version :param version: taxii version key (either 2.0 or 2.1) """ server_url = urljoin(self.base_url) self._conn = _HTTPConnection(verify=self.verify, proxies=self.proxies, version=version, auth=self.auth, cert=self.crt) if self.auth_header: # add auth_header to the session object self._conn.session.headers = ( # type: ignore[attr-defined] merge_setting( self._conn.session.headers, # type: ignore[attr-defined] {self.auth_header: self.auth_key}, dict_class=CaseInsensitiveDict, ), ) if version is TAXII_VER_2_0: self.server = v20.Server( server_url, verify=self.verify, proxies=self.proxies, conn=self._conn, ) else: self.server = v21.Server( server_url, verify=self.verify, proxies=self.proxies, conn=self._conn, )
def test_header_merge_none(): conn = _HTTPConnection() headers = conn._merge_headers(None) assert headers == { "user-agent": DEFAULT_USER_AGENT }
def test_header_merging(): conn = _HTTPConnection() headers = conn._merge_headers({"AddedHeader": "addedvalue"}) assert headers == { "user-agent": DEFAULT_USER_AGENT, "addedheader": "addedvalue" }
def test_invalid_accept_for_connection(): responses.add(responses.GET, COLLECTION_URL, COLLECTIONS_RESPONSE, status=406, content_type=MEDIA_TYPE_TAXII_V21) with pytest.raises(requests.exceptions.HTTPError): conn = _HTTPConnection(user="******", password="******", verify=False) conn.get("https://example.com/api1/collections/91a7b528-80eb-42ed-a74d-c6fbd5a26116/", headers={"Accept": "application/taxii+json; version=2.1"})
def test_valid_content_type_for_connection(): """The server responded with charset=utf-8, but the media types are correct and first.""" responses.add(responses.GET, COLLECTION_URL, COLLECTIONS_RESPONSE, status=200, content_type=MEDIA_TYPE_TAXII_V21 + "; charset=utf-8") conn = _HTTPConnection(user="******", password="******", verify=False, version="2.1") conn.get("https://example.com/api1/collections/91a7b528-80eb-42ed-a74d-c6fbd5a26116/", headers={"Accept": MEDIA_TYPE_TAXII_V21})
def test_conn_post_kwarg_errors(): conn = _HTTPConnection() with pytest.raises(InvalidArgumentsError): conn.post(DISCOVERY_URL, data=1, json=2) with pytest.raises(InvalidArgumentsError): conn.post(DISCOVERY_URL, data=1, foo=2) with pytest.raises(InvalidArgumentsError): conn.post(DISCOVERY_URL, foo=1)
def test_invalid_content_type_for_connection(): responses.add(responses.GET, COLLECTION_URL, COLLECTIONS_RESPONSE, status=200, content_type=MEDIA_TYPE_TAXII_V21) with pytest.raises(TAXIIServiceException) as excinfo: conn = _HTTPConnection(user="******", password="******", verify=False) conn.get("https://example.com/api1/collections/91a7b528-80eb-42ed-a74d-c6fbd5a26116/", headers={"Accept": MEDIA_TYPE_TAXII_V21 + "; charset=utf-8"}) assert ("Unexpected Response. Got Content-Type: 'application/taxii+json; " "version=2.1' for Accept: 'application/taxii+json; version=2.1; " "charset=utf-8'") in str(excinfo.value)
def test_user_agent_enforcing3(): conn = _HTTPConnection(user_agent=None) headers = conn._merge_headers({"User-Agent": None}) assert headers["user-agent"] == DEFAULT_USER_AGENT
def test_user_agent_overriding(): conn = _HTTPConnection(user_agent="foo/1.0") headers = conn._merge_headers({"User-Agent": "bar/2.0"}) assert headers["user-agent"] == "bar/2.0"
def test_user_agent_defaulting(): conn = _HTTPConnection(user_agent="foo/1.0") headers = conn._merge_headers({}) # also test key access is case-insensitive assert headers["user-agent"] == "foo/1.0"