2015-06-30 17:49:00 +00:00
|
|
|
package httputils
|
|
|
|
|
2015-08-28 15:03:04 +00:00
|
|
|
import "testing"
|
2015-06-30 17:49:00 +00:00
|
|
|
|
|
|
|
func TestDownload(t *testing.T) {
|
|
|
|
_, err := Download("http://docker.com")
|
|
|
|
|
|
|
|
if err != nil {
|
2015-08-28 15:03:04 +00:00
|
|
|
t.Fatalf("Expected error to not exist when Download(http://docker.com)")
|
2015-06-30 17:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expected status code = 404
|
2015-07-01 15:42:13 +00:00
|
|
|
if _, err = Download("http://docker.com/abc1234567"); err == nil {
|
2015-08-28 15:03:04 +00:00
|
|
|
t.Fatalf("Expected error to exist when Download(http://docker.com/abc1234567)")
|
2015-06-30 17:49:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewHTTPRequestError(t *testing.T) {
|
|
|
|
errorMessage := "Some error message"
|
|
|
|
httpResponse, _ := Download("http://docker.com")
|
2015-07-01 15:42:13 +00:00
|
|
|
if err := NewHTTPRequestError(errorMessage, httpResponse); err.Error() != errorMessage {
|
2015-08-28 15:03:04 +00:00
|
|
|
t.Fatalf("Expected err to equal error Message")
|
2015-06-30 17:49:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseServerHeader(t *testing.T) {
|
2015-08-28 15:03:04 +00:00
|
|
|
if _, err := ParseServerHeader("bad header"); err != errInvalidHeader {
|
|
|
|
t.Fatalf("Should fail when header can not be parsed")
|
2015-06-30 17:49:00 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:03:04 +00:00
|
|
|
if _, err := ParseServerHeader("(bad header)"); err != errInvalidHeader {
|
|
|
|
t.Fatalf("Should fail when header can not be parsed")
|
2015-06-30 17:49:00 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:03:04 +00:00
|
|
|
if _, err := ParseServerHeader("(without/spaces)"); err != errInvalidHeader {
|
|
|
|
t.Fatalf("Should fail when header can not be parsed")
|
2015-06-30 17:49:00 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:03:04 +00:00
|
|
|
if _, err := ParseServerHeader("(header/with space)"); err != errInvalidHeader {
|
|
|
|
t.Fatalf("Expected err to not exist when ParseServerHeader(\"(header/with space)\")")
|
2015-07-01 15:42:13 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:03:04 +00:00
|
|
|
serverHeader, err := ParseServerHeader("foo/bar (baz)")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if serverHeader.App != "foo" {
|
|
|
|
t.Fatalf("Expected serverHeader.App to equal \"foo\", got %s", serverHeader.App)
|
2015-06-30 17:49:00 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:03:04 +00:00
|
|
|
if serverHeader.Ver != "bar" {
|
|
|
|
t.Fatalf("Expected serverHeader.Ver to equal \"bar\", got %s", serverHeader.Ver)
|
2015-06-30 17:49:00 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:03:04 +00:00
|
|
|
if serverHeader.OS != "baz" {
|
|
|
|
t.Fatalf("Expected serverHeader.OS to equal \"baz\", got %s", serverHeader.OS)
|
2015-06-30 17:49:00 +00:00
|
|
|
}
|
|
|
|
}
|