Python twisted.web.http_headers.Headers() Examples
The following are 30
code examples of twisted.web.http_headers.Headers().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
twisted.web.http_headers
, or try the search function
.
Example #1
Source File: test_agent.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _testRedirectURI(self, uri, location, finalURI): """ When L{client.RedirectAgent} encounters a relative redirect I{URI}, it is resolved against the request I{URI} before following the redirect. @param uri: Request URI. @param location: I{Location} header redirect URI. @param finalURI: Expected final URI. """ self.agent.request(b'GET', uri) req, res = self.protocol.requests.pop() headers = http_headers.Headers( {b'location': [location]}) response = Response((b'HTTP', 1, 1), 302, b'OK', headers, None) res.callback(response) req2, res2 = self.protocol.requests.pop() self.assertEqual(b'GET', req2.method) self.assertEqual(finalURI, req2.absoluteURI)
Example #2
Source File: test_distrib.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_requestHeaders(self): """ The request headers are available on the request object passed to a distributed resource's C{render} method. """ requestHeaders = {} class ReportRequestHeaders(resource.Resource): def render(self, request): requestHeaders.update(dict( request.requestHeaders.getAllRawHeaders())) return "" request = self._requestTest( ReportRequestHeaders(), headers=Headers({'foo': ['bar']})) def cbRequested(result): self.assertEqual(requestHeaders['Foo'], ['bar']) request.addCallback(cbRequested) return request
Example #3
Source File: http.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def writeHeaders(self, version, code, reason, headers): """ Called by L{Request} objects to write a complete set of HTTP headers to a transport. @param version: The HTTP version in use. @type version: L{bytes} @param code: The HTTP status code to write. @type code: L{bytes} @param reason: The HTTP reason phrase to write. @type reason: L{bytes} @param headers: The headers to write to the transport. @type headers: L{twisted.web.http_headers.Headers} """ responseLine = version + b" " + code + b" " + reason + b"\r\n" headerSequence = [responseLine] headerSequence.extend( name + b': ' + value + b"\r\n" for name, value in headers ) headerSequence.append(b"\r\n") self.transport.writeSequence(headerSequence)
Example #4
Source File: http.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def __init__(self, channel, queued=_QUEUED_SENTINEL): """ @param channel: the channel we're connected to. @param queued: (deprecated) are we in the request queue, or can we start writing to the transport? """ self.notifications = [] self.channel = channel self.requestHeaders = Headers() self.received_cookies = {} self.responseHeaders = Headers() self.cookies = [] # outgoing cookies self.transport = self.channel.transport if queued is _QUEUED_SENTINEL: queued = False self.queued = queued
Example #5
Source File: client.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def __init__(self, uri): """ Create a fake Urllib2 request. @param uri: Request URI. @type uri: L{bytes} """ self.uri = nativeString(uri) self.headers = Headers() _uri = URI.fromBytes(uri) self.type = nativeString(_uri.scheme) self.host = nativeString(_uri.host) if (_uri.scheme, _uri.port) not in ((b'http', 80), (b'https', 443)): # If it's not a schema on the regular port, add the port. self.host += ":" + str(_uri.port) if _PY3: self.origin_req_host = nativeString(_uri.host) self.unverifiable = lambda _: False
Example #6
Source File: test_gcmclient.py From autopush with Mozilla Public License 2.0 | 6 votes |
def setUp(self): self.gcm = gcmclient.GCM(api_key="FakeValue") self.gcm._sender = Mock(spec=treq.request) self._m_request = Deferred() self.gcm._sender.return_value = self._m_request self._m_response = Mock(spec=treq.response._Response) self._m_response.code = 200 self._m_response.headers = Headers() self._m_resp_text = Deferred() self._m_response.text.return_value = self._m_resp_text self.m_payload = gcmclient.JSONMessage( registration_ids="some_reg_id", collapse_key="coll_key", time_to_live=60, dry_run=False, data={"foo": "bar"} )
Example #7
Source File: test_infrastructure.py From flocker with Apache License 2.0 | 6 votes |
def test_serialized_task(self, logger): """ If the ``X-Eliot-Task-Id`` header containers a serialized task level, it is used as a parent of the logged actions from the handler. """ parent = ActionType("parent", [], []) with parent() as context: task_id = context.serialize_task_id() app = self.Application() app.logger = logger request = dummyRequest( b"GET", b"/foo/bar", Headers({b"X-Eliot-Task-Id": [task_id]}), b"") render(app.app.resource(), request) logged_parent = LoggedAction.ofType(logger.messages, parent)[0] child = _assertRequestLogged(b"/foo/bar")(self, logger) self.assertIn(child, logged_parent.descendants())
Example #8
Source File: test_agent.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_headersUnmodified(self): """ If a I{Host} header must be added to the request, the L{Headers} instance passed to L{Agent.request} is not modified. """ headers = http_headers.Headers() self.agent._getEndpoint = lambda *args: self self.agent.request( b'GET', b'http://example.com/foo', headers) protocol = self.protocol # The request should have been issued. self.assertEqual(len(protocol.requests), 1) # And the headers object passed in should not have changed. self.assertEqual(headers, http_headers.Headers())
Example #9
Source File: test_infrastructure.py From flocker with Apache License 2.0 | 6 votes |
def test_responseValidationError(self, logger): """ If the response body doesn't match the provided schema, then the request automatically receives a I{INTERNAL SERVER ERROR} response. This test fails if ``_validate_responses == False``. Hence it also confirms that validation is enabled for other tests. """ request = dummyRequest( b"GET", b"/foo/badresponse", Headers({b"content-type": [b"application/json"]}), b"") app = self.Application(logger, None) render(app.app.resource(), request) self.assertEqual(request._code, INTERNAL_SERVER_ERROR)
Example #10
Source File: test_infrastructure.py From flocker with Apache License 2.0 | 6 votes |
def test_validationError(self, logger): """ If the request body doesn't match the provided schema, then the request automatically receives a I{BAD REQUEST} response. """ request = dummyRequest( b"PUT", b"/foo/validation", Headers({b"content-type": [b"application/json"]}), dumps({u'int': []})) app = self.Application(logger, None) render(app.app.resource(), request) response = loads(request._responseBody) self.assertEqual( (request._code, response[u'description'], len(response[u'errors'])), (BAD_REQUEST, FAILED_INPUT_VALIDATION, 2))
Example #11
Source File: test_agent.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_noRedirect(self): """ L{client.RedirectAgent} behaves like L{client.Agent} if the response doesn't contain a redirect. """ deferred = self.agent.request(b'GET', b'http://example.com/foo') req, res = self.protocol.requests.pop() headers = http_headers.Headers() response = Response((b'HTTP', 1, 1), 200, b'OK', headers, None) res.callback(response) self.assertEqual(0, len(self.protocol.requests)) result = self.successResultOf(deferred) self.assertIdentical(response, result) self.assertIdentical(result.previousResponse, None)
Example #12
Source File: test_infrastructure.py From flocker with Apache License 2.0 | 6 votes |
def test_malformedRequest(self, logger): """ If the request body cannot be decoded as a I{JSON} blob then the request automatically receives a I{BAD REQUEST} response. """ app = self.Application(logger, None) request = dummyRequest( b"PUT", b"/foo/bar", Headers({b"content-type": [b"application/json"]}), b"foo bar") render(app.app.resource(), request) # The endpoint should not have been called. self.assertIs(None, app.kwargs) expected = CloseEnoughJSONResponse( BAD_REQUEST, Headers({b"content-type": [b"application/json"]}), {u"description": DECODING_ERROR_DESCRIPTION}) return expected.verify(asResponse(request))
Example #13
Source File: test_agent.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _testRedirectToGet(self, code, method): """ L{client.RedirectAgent} changes the method to I{GET} when getting a redirect on a non-I{GET} request. @param code: HTTP status code. @param method: HTTP request method. """ self.agent.request(method, b'http://example.com/foo') req, res = self.protocol.requests.pop() headers = http_headers.Headers( {b'location': [b'http://example.com/bar']}) response = Response((b'HTTP', 1, 1), code, b'OK', headers, None) res.callback(response) req2, res2 = self.protocol.requests.pop() self.assertEqual(b'GET', req2.method) self.assertEqual(b'/bar', req2.uri)
Example #14
Source File: test_agent.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_noLocationField(self): """ If no L{Location} header field is found when getting a redirect, L{client.RedirectAgent} fails with a L{ResponseFailed} error wrapping a L{error.RedirectWithNoLocation} exception. """ deferred = self.agent.request(b'GET', b'http://example.com/foo') req, res = self.protocol.requests.pop() headers = http_headers.Headers() response = Response((b'HTTP', 1, 1), 301, b'OK', headers, None) res.callback(response) fail = self.failureResultOf(deferred, client.ResponseFailed) fail.value.reasons[0].trap(error.RedirectWithNoLocation) self.assertEqual(b'http://example.com/foo', fail.value.reasons[0].value.uri) self.assertEqual(301, fail.value.response.code)
Example #15
Source File: test_agent.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _testPageRedirectFailure(self, code, method): """ When getting a redirect on an unsupported request method, L{client.RedirectAgent} fails with a L{ResponseFailed} error wrapping a L{error.PageRedirect} exception. @param code: HTTP status code. @param method: HTTP request method. """ deferred = self.agent.request(method, b'http://example.com/foo') req, res = self.protocol.requests.pop() headers = http_headers.Headers() response = Response((b'HTTP', 1, 1), code, b'OK', headers, None) res.callback(response) fail = self.failureResultOf(deferred, client.ResponseFailed) fail.value.reasons[0].trap(error.PageRedirect) self.assertEqual(b'http://example.com/foo', fail.value.reasons[0].value.location) self.assertEqual(code, fail.value.response.code)
Example #16
Source File: client.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _requestWithEndpoint(self, key, endpoint, method, parsedURI, headers, bodyProducer, requestPath): """ Issue a new request, given the endpoint and the path sent as part of the request. """ # Create minimal headers, if necessary: if headers is None: headers = Headers() if not headers.hasHeader(b'host'): headers = headers.copy() headers.addRawHeader( b'host', self._computeHostValue(parsedURI.scheme, parsedURI.host, parsedURI.port)) d = self._pool.getConnection(key, endpoint) def cbConnected(proto): return proto.request( Request._construct(method, requestPath, headers, bodyProducer, persistent=self._pool.persistent, parsedURI=parsedURI)) d.addCallback(cbConnected) return d
Example #17
Source File: test_agent.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_responseHistory(self): """ L{Response.response} references the previous L{Response} from a redirect, or L{None} if there was no previous response. """ agent = self.buildAgentForWrapperTest(self.reactor) redirectAgent = client.RedirectAgent(agent) deferred = redirectAgent.request(b'GET', b'http://example.com/foo') redirectReq, redirectRes = self.protocol.requests.pop() headers = http_headers.Headers( {b'location': [b'http://example.com/bar']}) redirectResponse = Response((b'HTTP', 1, 1), 302, b'OK', headers, None) redirectRes.callback(redirectResponse) req, res = self.protocol.requests.pop() response = Response((b'HTTP', 1, 1), 200, b'OK', headers, None) res.callback(response) finalResponse = self.successResultOf(deferred) self.assertIdentical(finalResponse.previousResponse, redirectResponse) self.assertIdentical(redirectResponse.previousResponse, None)
Example #18
Source File: test_infrastructure.py From flocker with Apache License 2.0 | 6 votes |
def assertNoDecodeLogged(self, logger, method, path=b"/foo/bar", content_type=b"application/json"): """ The I{JSON}-encoded request body is ignored when the given method is used. @param method: A HTTP method. @type method: L{bytes} @param path: Path to request. @type path: L{bytes} @param content_type: Content type to send, by default I{application/json}. """ objects = {"foo": "bar", "baz": ["quux"]} request = dummyRequest( method, path, Headers({b"content-type": [content_type]}), dumps(objects)) app = self.Application(logger, None) render(app.app.resource(), request) self.assertEqual({}, app.kwargs)
Example #19
Source File: test_cgi.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_noProxyPassthrough(self): """ The CGI script is never called with the Proxy header passed through. """ cgiFilename = self.writeCGI(HEADER_OUTPUT_CGI) portnum = self.startServer(cgiFilename) url = "http://localhost:%d/cgi" % (portnum,) agent = client.Agent(reactor) headers = http_headers.Headers({"Proxy": ["foo"], "X-Innocent-Header": ["bar"]}) d = agent.request(b"GET", url, headers=headers) def checkResponse(response): headers = json.loads(response) self.assertEqual( set(headers.keys()), {"HTTP_HOST", "HTTP_CONNECTION", "HTTP_X_INNOCENT_HEADER"}) d.addCallback(client.readBody) d.addCallback(checkResponse) return d
Example #20
Source File: test_http.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _compatHeadersTest(self, oldName, newName): """ Verify that each of two different attributes which are associated with the same state properly reflect changes made through the other. This is used to test that the C{headers}/C{responseHeaders} and C{received_headers}/C{requestHeaders} pairs interact properly. """ req = http.Request(DummyChannel(), False) getattr(req, newName).setRawHeaders(b"test", [b"lemur"]) self.assertEqual(getattr(req, oldName)[b"test"], b"lemur") setattr(req, oldName, {b"foo": b"bar"}) self.assertEqual( list(getattr(req, newName).getAllRawHeaders()), [(b"Foo", [b"bar"])]) setattr(req, newName, http_headers.Headers()) self.assertEqual(getattr(req, oldName), {})
Example #21
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_responseHeaders(self): """ The response headers are added to the response object's C{headers} L{Headers} instance. """ protocol = HTTPClientParser( Request(b'GET', b'/', _boringHeaders, None), lambda rest: None) protocol.makeConnection(StringTransport()) protocol.dataReceived(b'HTTP/1.1 200 OK\r\n') protocol.dataReceived(b'X-Foo: bar\r\n') protocol.dataReceived(b'\r\n') self.assertEqual( protocol.connHeaders, Headers({})) self.assertEqual( protocol.response.headers, Headers({b'x-foo': [b'bar']})) self.assertIdentical(protocol.response.length, UNKNOWN_LENGTH)
Example #22
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_connectionHeaders(self): """ The connection control headers are added to the parser's C{connHeaders} L{Headers} instance. """ protocol = HTTPClientParser( Request(b'GET', b'/', _boringHeaders, None), lambda rest: None) protocol.makeConnection(StringTransport()) protocol.dataReceived(b'HTTP/1.1 200 OK\r\n') protocol.dataReceived(b'Content-Length: 123\r\n') protocol.dataReceived(b'Connection: close\r\n') protocol.dataReceived(b'\r\n') self.assertEqual( protocol.response.headers, Headers({})) self.assertEqual( protocol.connHeaders, Headers({b'content-length': [b'123'], b'connection': [b'close']})) self.assertEqual(protocol.response.length, 123)
Example #23
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_headResponseContentLengthEntityHeader(self): """ If a HEAD request is made, the I{Content-Length} header in the response is added to the response headers, not the connection control headers. """ protocol = HTTPClientParser( Request(b'HEAD', b'/', _boringHeaders, None), lambda rest: None) protocol.makeConnection(StringTransport()) protocol.dataReceived(b'HTTP/1.1 200 OK\r\n') protocol.dataReceived(b'Content-Length: 123\r\n') protocol.dataReceived(b'\r\n') self.assertEqual( protocol.response.headers, Headers({b'content-length': [b'123']})) self.assertEqual( protocol.connHeaders, Headers({})) self.assertEqual(protocol.response.length, 0)
Example #24
Source File: test_infrastructure.py From flocker with Apache License 2.0 | 6 votes |
def test_responseResultContainsIncident(self, logger): """ If the decorated function raises an exception, the HTTP response body is a json-encoded object with a item set to an incident identifier. """ request = dummyRequest(b"GET", b"/foo/exception", Headers(), b"") app = self.application(logger, None) self.render(app.app.resource(), request) logger.flushTracebacks(ArbitraryException) incident = loads(request._responseBody) action = LoggedAction.ofType(logger.messages, REQUEST)[0] # First message in action was action start with task_level /1, so # next task_level is /2: self.assertEqual( u"{}@/2".format(action.startMessage[u"task_uuid"]), incident)
Example #25
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_receiveSimplestResponse(self): """ When a response is delivered to L{HTTP11ClientProtocol}, the L{Deferred} previously returned by the C{request} method is called back with a L{Response} instance and the connection is closed. """ d = self.protocol.request(Request(b'GET', b'/', _boringHeaders, None)) def cbRequest(response): self.assertEqual(response.code, 200) self.assertEqual(response.headers, Headers()) self.assertTrue(self.transport.disconnecting) self.assertEqual(self.protocol.state, u'QUIESCENT') d.addCallback(cbRequest) self.protocol.dataReceived( b"HTTP/1.1 200 OK\r\n" b"Content-Length: 0\r\n" b"Connection: close\r\n" b"\r\n") return d
Example #26
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_receiveResponseHeaders(self): """ The headers included in a response delivered to L{HTTP11ClientProtocol} are included on the L{Response} instance passed to the callback returned by the C{request} method. """ d = self.protocol.request(Request(b'GET', b'/', _boringHeaders, None)) def cbRequest(response): expected = Headers({b'x-foo': [b'bar', b'baz']}) self.assertEqual(response.headers, expected) d.addCallback(cbRequest) self.protocol.dataReceived( b"HTTP/1.1 200 OK\r\n" b"X-Foo: bar\r\n" b"X-Foo: baz\r\n" b"\r\n") return d
Example #27
Source File: test_infrastructure.py From flocker with Apache License 2.0 | 6 votes |
def test_badRequestRaised(self, logger): """ If the decorated function raises L{BadRequest} then the generated response: - has the code taken from the exception. - has a I{Content-Type} header set to I{application/json}. - has a JSON-encoded body indicating an error response and giving details from the exception. """ application = self.application(logger, {}) request = dummyRequest(b"GET", b"/foo/badrequest", Headers(), b"") self.render(application.app.resource(), request) expected = CloseEnoughJSONResponse( application.BAD_REQUEST_CODE, Headers({b"content-type": [b"application/json"]}), application.BAD_REQUEST_RESULT) return expected.verify(asResponse(request))
Example #28
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_sendRequestHeaders(self): """ L{Request.writeTo} formats header data and writes it to the given transport. """ headers = Headers({b'x-foo': [b'bar', b'baz'], b'host': [b'example.com']}) Request(b'GET', b'/foo', headers, None).writeTo(self.transport) lines = self.transport.value().split(b'\r\n') self.assertEqual(lines[0], b"GET /foo HTTP/1.1") self.assertEqual(lines[-2:], [b"", b""]) del lines[0], lines[-2:] lines.sort() self.assertEqual( lines, [b"Connection: close", b"Host: example.com", b"X-Foo: bar", b"X-Foo: baz"])
Example #29
Source File: testtools.py From flocker with Apache License 2.0 | 6 votes |
def __init__(self, code, headers, body): """ @param code: The expected HTTP response code. @type code: L{int} @param headers: The minimum set of headers which must be present in the response. @type headers: L{twisted.web.http_headers.Headers} @param body: The structured form of the body expected in the response. This is compared against the received body after the received body is decoded with C{self.decode}. """ self.code = code self.headers = headers self.body = body
Example #30
Source File: test_infrastructure.py From flocker with Apache License 2.0 | 6 votes |
def test_explicitResponseObject(self, logger): """ If the return value of the decorated function is an instance of L{EndpointResponse} then the response generated: - has the code given by the L{EndpointResponse} instance. - has a I{Content-Type} header set to I{application/json}. - has a JSON-encoded body indicating a successful response and giving the result from the L{EndpointResponse} instance. """ application = self.application(logger, {}) request = dummyRequest( b"GET", b"/foo/explicitresponse", Headers(), b"") self.render(application.app.resource(), request) expected = CloseEnoughJSONResponse( application.EXPLICIT_RESPONSE_CODE, Headers({b"content-type": [b"application/json"]}), application.EXPLICIT_RESPONSE_RESULT) return expected.verify(asResponse(request))