2015-06-30 17:49:00 +00:00
|
|
|
package httputils
|
|
|
|
|
2015-09-09 12:59:25 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
2015-06-30 17:49:00 +00:00
|
|
|
|
|
|
|
func TestDownload(t *testing.T) {
|
2015-09-09 12:59:25 +00:00
|
|
|
expected := "Hello, docker !"
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, expected)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
response, err := Download(ts.URL)
|
2015-06-30 17:49:00 +00:00
|
|
|
if err != nil {
|
2015-09-09 12:59:25 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := ioutil.ReadAll(response.Body)
|
|
|
|
response.Body.Close()
|
|
|
|
|
|
|
|
if err != nil || string(actual) != expected {
|
2017-01-17 16:10:40 +00:00
|
|
|
t.Fatalf("Expected the response %q, got err:%q, actual:%q", expected, err, string(actual))
|
2015-09-09 12:59:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownload400Errors(t *testing.T) {
|
|
|
|
expectedError := "Got HTTP status code >= 400: 403 Forbidden"
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// 403
|
|
|
|
http.Error(w, "something failed (forbidden)", http.StatusForbidden)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
// Expected status code = 403
|
|
|
|
if _, err := Download(ts.URL); err == nil || err.Error() != expectedError {
|
2016-12-21 03:03:39 +00:00
|
|
|
t.Fatalf("Expected the error %q, got %q", expectedError, err)
|
2015-06-30 17:49:00 +00:00
|
|
|
}
|
2015-09-09 12:59:25 +00:00
|
|
|
}
|
2015-06-30 17:49:00 +00:00
|
|
|
|
2015-09-09 12:59:25 +00:00
|
|
|
func TestDownloadOtherErrors(t *testing.T) {
|
|
|
|
if _, err := Download("I'm not an url.."); err == nil || !strings.Contains(err.Error(), "unsupported protocol scheme") {
|
2016-12-21 03:03:39 +00:00
|
|
|
t.Fatalf("Expected an error with 'unsupported protocol scheme', got %q", err)
|
2015-06-30 17:49:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewHTTPRequestError(t *testing.T) {
|
|
|
|
errorMessage := "Some error message"
|
2015-09-09 12:59:25 +00:00
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// 403
|
|
|
|
http.Error(w, errorMessage, http.StatusForbidden)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
httpResponse, err := http.Get(ts.URL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-01 15:42:13 +00:00
|
|
|
if err := NewHTTPRequestError(errorMessage, httpResponse); err.Error() != errorMessage {
|
2016-12-21 03:03:39 +00:00
|
|
|
t.Fatalf("Expected err to be %q, got %q", errorMessage, err)
|
2015-06-30 17:49:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseServerHeader(t *testing.T) {
|
2015-08-28 18:54:44 +00:00
|
|
|
inputs := map[string][]string{
|
|
|
|
"bad header": {"error"},
|
|
|
|
"(bad header)": {"error"},
|
|
|
|
"(without/spaces)": {"error"},
|
|
|
|
"(header/with spaces)": {"error"},
|
|
|
|
"foo/bar (baz)": {"foo", "bar", "baz"},
|
|
|
|
"foo/bar": {"error"},
|
|
|
|
"foo": {"error"},
|
|
|
|
"foo/bar (baz space)": {"foo", "bar", "baz space"},
|
|
|
|
" f f / b b ( b s ) ": {"f f", "b b", "b s"},
|
|
|
|
"foo/bar (baz) ignore": {"foo", "bar", "baz"},
|
|
|
|
"foo/bar ()": {"error"},
|
|
|
|
"foo/bar()": {"error"},
|
|
|
|
"foo/bar(baz)": {"foo", "bar", "baz"},
|
|
|
|
"foo/bar/zzz(baz)": {"foo/bar", "zzz", "baz"},
|
|
|
|
"foo/bar(baz/abc)": {"foo", "bar", "baz/abc"},
|
|
|
|
"foo/bar(baz (abc))": {"foo", "bar", "baz (abc)"},
|
2015-06-30 17:49:00 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 18:54:44 +00:00
|
|
|
for header, values := range inputs {
|
|
|
|
serverHeader, err := ParseServerHeader(header)
|
|
|
|
if err != nil {
|
|
|
|
if err != errInvalidHeader {
|
|
|
|
t.Fatalf("Failed to parse %q, and got some unexpected error: %q", header, err)
|
|
|
|
}
|
|
|
|
if values[0] == "error" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Fatalf("Header %q failed to parse when it shouldn't have", header)
|
|
|
|
}
|
|
|
|
if values[0] == "error" {
|
|
|
|
t.Fatalf("Header %q parsed ok when it should have failed(%q).", header, serverHeader)
|
|
|
|
}
|
2015-06-30 17:49:00 +00:00
|
|
|
|
2015-08-28 18:54:44 +00:00
|
|
|
if serverHeader.App != values[0] {
|
|
|
|
t.Fatalf("Expected serverHeader.App for %q to equal %q, got %q", header, values[0], serverHeader.App)
|
|
|
|
}
|
2015-07-01 15:42:13 +00:00
|
|
|
|
2015-08-28 18:54:44 +00:00
|
|
|
if serverHeader.Ver != values[1] {
|
|
|
|
t.Fatalf("Expected serverHeader.Ver for %q to equal %q, got %q", header, values[1], serverHeader.Ver)
|
|
|
|
}
|
2015-08-28 15:03:04 +00:00
|
|
|
|
2015-08-28 18:54:44 +00:00
|
|
|
if serverHeader.OS != values[2] {
|
|
|
|
t.Fatalf("Expected serverHeader.OS for %q to equal %q, got %q", header, values[2], serverHeader.OS)
|
|
|
|
}
|
2015-06-30 17:49:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|