This repository was archived by the owner on Nov 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
Set fake host header for local communication. #190
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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,77 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"golang.org/x/net/context" | ||
) | ||
|
||
// TestSetHostHeader should set fake host for local communications, set real host | ||
// for normal communications. | ||
func TestSetHostHeader(t *testing.T) { | ||
testURL := "/test" | ||
testCases := []struct { | ||
host string | ||
expectedHost string | ||
expectedURLHost string | ||
}{ | ||
{ | ||
"unix:///var/run/docker.sock", | ||
"docker", | ||
"/var/run/docker.sock", | ||
}, | ||
{ | ||
"npipe:////./pipe/docker_engine", | ||
"docker", | ||
"//./pipe/docker_engine", | ||
}, | ||
{ | ||
"tcp://0.0.0.0:4243", | ||
"", | ||
"0.0.0.0:4243", | ||
}, | ||
{ | ||
"tcp://localhost:4243", | ||
"", | ||
"localhost:4243", | ||
}, | ||
} | ||
|
||
for c, test := range testCases { | ||
proto, addr, basePath, err := ParseHost(test.host) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
client := &Client{ | ||
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { | ||
if !strings.HasPrefix(req.URL.Path, testURL) { | ||
return nil, fmt.Errorf("Test Case #%d: Expected URL %q, got %q", c, testURL, req.URL) | ||
} | ||
if req.Host != test.expectedHost { | ||
return nil, fmt.Errorf("Test Case #%d: Expected host %q, got %q", c, test.expectedHost, req.Host) | ||
} | ||
if req.URL.Host != test.expectedURLHost { | ||
return nil, fmt.Errorf("Test Case #%d: Expected URL host %q, got %q", c, test.expectedURLHost, req.URL.Host) | ||
} | ||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: ioutil.NopCloser(bytes.NewReader(([]byte("")))), | ||
}, nil | ||
}), | ||
proto: proto, | ||
addr: addr, | ||
basePath: basePath, | ||
} | ||
|
||
_, err = client.sendRequest(context.Background(), "GET", testURL, nil, nil, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
} |
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.
Even though the PR is focused on the
""
case, let's make the testcase cover the "normal" host cases too. Can you please add a test variant wheretest.expectedHost
isn't""
?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 think the
test.expectedHost
is always""
now.Now we just pass the apiPath (something without schema like
/container/id/start
) intohttp.NewRequest
. (See here)While
http.NewRequest
only setsrequest.Host
when the passed in url has Schema. (See here, here and here).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.
Oh, wait, let me see.
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.
wouldn't using "tcp://localhost:4243" do it?
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.
No. The
request.Host
is still""
with "tcp://localhost:4243".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.
oh I also added:
around like 92 in request.go
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.
oh yep, I think you're right @duglin it would be better to use
NewClient
everywhere.newMockClient
tonewMockTransportClient
(ornewMockTransport
…) as it's more what it does.newMockClient
that does callNewClient
, …I would even think we should check the error on
NewClient
.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.
@duglin, I think the main reason is that you added
req.Host = host
in request.go, there is nothing to do with theNewClient
.The
req.Host
is initialized inhttp.NewRequest
before, and mostly it will be set to "" except the bug I mentioned above.Now you manually added
req.Host = host
, of course it will be equal toreq.Host.URL
./cc @vdemeester
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.
Something feels wrong here. According to this test the HTTP
Host
header will always be empty, which doesn't seem right to me. If it really is supposed to be blank then why include the string in the test data, just check for a static""
in the if-statement. However, having an empty header is not correct and we should fix that.I'll try to find some time to look at this but kind of busy today....
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.
@duglin Yeah, you are right. If it is supposed to be blank, we should just check it with a static
""
. There is indeed something wrong here, wait for your result. :)