Signed-off-by: Kristina Zabunova <triara.xiii@gmail.com>
(cherry picked from commit d71817464e859cd323d6cdaf4dec5a9dd530f57e)
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,59 @@ |
| 0 |
+package httputils |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "testing" |
|
| 4 |
+) |
|
| 5 |
+ |
|
| 6 |
+func TestDownload(t *testing.T) {
|
|
| 7 |
+ _, err := Download("http://docker.com")
|
|
| 8 |
+ |
|
| 9 |
+ if err != nil {
|
|
| 10 |
+ t.Errorf("Expected error to not exist when Download(http://docker.com)")
|
|
| 11 |
+ } |
|
| 12 |
+ |
|
| 13 |
+ // Expected status code = 404 |
|
| 14 |
+ _, err = Download("http://docker.com/abc1234567")
|
|
| 15 |
+ if err == nil {
|
|
| 16 |
+ t.Errorf("Expected error to exist when Download(http://docker.com/abc1234567)")
|
|
| 17 |
+ } |
|
| 18 |
+} |
|
| 19 |
+ |
|
| 20 |
+func TestNewHTTPRequestError(t *testing.T) {
|
|
| 21 |
+ errorMessage := "Some error message" |
|
| 22 |
+ httpResponse, _ := Download("http://docker.com")
|
|
| 23 |
+ err := NewHTTPRequestError(errorMessage, httpResponse) |
|
| 24 |
+ |
|
| 25 |
+ if err.Error() != errorMessage {
|
|
| 26 |
+ t.Errorf("Expected err to equal error Message")
|
|
| 27 |
+ } |
|
| 28 |
+} |
|
| 29 |
+ |
|
| 30 |
+func TestParseServerHeader(t *testing.T) {
|
|
| 31 |
+ serverHeader, err := ParseServerHeader("bad header")
|
|
| 32 |
+ if err.Error() != "Bad header: Failed regex match" {
|
|
| 33 |
+ t.Errorf("Should fail when header can not be parsed")
|
|
| 34 |
+ } |
|
| 35 |
+ |
|
| 36 |
+ serverHeader, err = ParseServerHeader("(bad header)")
|
|
| 37 |
+ if err.Error() != "Bad header: '/' missing" {
|
|
| 38 |
+ t.Errorf("Should fail when header can not be parsed")
|
|
| 39 |
+ } |
|
| 40 |
+ |
|
| 41 |
+ serverHeader, err = ParseServerHeader("(without/spaces)")
|
|
| 42 |
+ if err.Error() != "Bad header: Expected single space" {
|
|
| 43 |
+ t.Errorf("Should fail when header can not be parsed")
|
|
| 44 |
+ } |
|
| 45 |
+ |
|
| 46 |
+ serverHeader, err = ParseServerHeader("(header/with space)")
|
|
| 47 |
+ if serverHeader.App != "(header" {
|
|
| 48 |
+ t.Errorf("Expected serverHeader.App to equal \"(header\"")
|
|
| 49 |
+ } |
|
| 50 |
+ |
|
| 51 |
+ if serverHeader.Ver != "with" {
|
|
| 52 |
+ t.Errorf("Expected serverHeader.Ver to equal \"with\"")
|
|
| 53 |
+ } |
|
| 54 |
+ |
|
| 55 |
+ if serverHeader.OS != "header/with space" {
|
|
| 56 |
+ t.Errorf("Expected serverHeader.OS to equal \"header/with space\"")
|
|
| 57 |
+ } |
|
| 58 |
+} |
| 0 | 59 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,14 @@ |
| 0 |
+package httputils |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "testing" |
|
| 4 |
+) |
|
| 5 |
+ |
|
| 6 |
+func TestDetectContentType(t *testing.T) {
|
|
| 7 |
+ input := []byte("That is just a plain text")
|
|
| 8 |
+ |
|
| 9 |
+ contentType, _, err := DetectContentType(input) |
|
| 10 |
+ if err != nil || contentType != "text/plain" {
|
|
| 11 |
+ t.Errorf("TestDetectContentType failed")
|
|
| 12 |
+ } |
|
| 13 |
+} |