-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preserve individual headers with the same name on responses #1208
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ee6009a
Preserve individual headers with the same name on responses
isidentical 47311d1
Rename RequestHeadersDict to HTTPHeadersDict
isidentical 7ef349e
Update tests/utils/http_server.py
jkbrzt fb66eb5
Update tests/utils/http_server.py
jkbrzt b0c55b0
Update httpie/adapters.py
jkbrzt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from httpie.cli.dicts import HTTPHeadersDict | ||
from requests.adapters import HTTPAdapter | ||
|
||
|
||
class HTTPieHTTPAdapter(HTTPAdapter): | ||
|
||
def build_response(self, req, resp): | ||
"""Wrap the original headers with the `HTTPHeadersDict` | ||
to preserve multiple headers that have the same name""" | ||
|
||
response = super().build_response(req, resp) | ||
response.headers = HTTPHeadersDict(getattr(resp, 'headers', {})) | ||
return response | ||
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
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import threading | ||
|
||
from collections import defaultdict | ||
from http import HTTPStatus | ||
from http.server import HTTPServer, BaseHTTPRequestHandler | ||
from urllib.parse import urlparse | ||
|
||
import pytest | ||
|
||
|
||
class TestHandler(BaseHTTPRequestHandler): | ||
handlers = defaultdict(dict) | ||
|
||
@classmethod | ||
def handler(cls, method, path): | ||
def inner(func): | ||
cls.handlers[method][path] = func | ||
return func | ||
return inner | ||
|
||
def do_GET(self): | ||
parse_result = urlparse(self.path) | ||
func = self.handlers['GET'].get(parse_result.path) | ||
if func is None: | ||
return self.send_error(HTTPStatus.NOT_FOUND) | ||
|
||
return func(self) | ||
|
||
|
||
@TestHandler.handler('GET', '/headers') | ||
def get_headers(handler): | ||
handler.send_response(200) | ||
for key, value in handler.headers.items(): | ||
handler.send_header(key, value) | ||
handler.send_header('Content-Length', 0) | ||
handler.end_headers() | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def http_server(): | ||
"""A custom HTTP server implementation for our tests, that is | ||
built on top of the http.server module. Handy when we need to | ||
deal with details which httpbin can not capture.""" | ||
|
||
server = HTTPServer(('localhost', 0), TestHandler) | ||
thread = threading.Thread(target=server.serve_forever) | ||
thread.start() | ||
yield '{}:{}'.format(*server.socket.getsockname()) | ||
server.shutdown() | ||
thread.join(timeout=0.5) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am thinking about moving
httpie/ssl.py
intohttpie/adapters.py
, since it does make sense for all the adapters to be in the same place. WDYT @jakubroztocil?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good 👍🏻