Kaynağa Gözat

unit test refactor in pkg/httputils as suggested by vdemeester; using pattern if x := …; x == nil {}

Signed-off-by: Kristina Zabunova <triara.xiii@gmail.com>
(cherry picked from commit c3f1b2a5bd4a4330bcbad401c316af90925b99ad)
Kristina Zabunova 10 yıl önce
ebeveyn
işleme
f753f6d597

+ 8 - 10
pkg/httputils/httputils_test.go

@@ -12,8 +12,7 @@ func TestDownload(t *testing.T) {
 	}
 	}
 
 
 	// Expected status code = 404
 	// Expected status code = 404
-	_, err = Download("http://docker.com/abc1234567")
-	if err == nil {
+	if _, err = Download("http://docker.com/abc1234567"); err == nil {
 		t.Errorf("Expected error to exist when Download(http://docker.com/abc1234567)")
 		t.Errorf("Expected error to exist when Download(http://docker.com/abc1234567)")
 	}
 	}
 }
 }
@@ -21,9 +20,7 @@ func TestDownload(t *testing.T) {
 func TestNewHTTPRequestError(t *testing.T) {
 func TestNewHTTPRequestError(t *testing.T) {
 	errorMessage := "Some error message"
 	errorMessage := "Some error message"
 	httpResponse, _ := Download("http://docker.com")
 	httpResponse, _ := Download("http://docker.com")
-	err := NewHTTPRequestError(errorMessage, httpResponse)
-
-	if err.Error() != errorMessage {
+	if err := NewHTTPRequestError(errorMessage, httpResponse); err.Error() != errorMessage {
 		t.Errorf("Expected err to equal error Message")
 		t.Errorf("Expected err to equal error Message")
 	}
 	}
 }
 }
@@ -34,17 +31,18 @@ func TestParseServerHeader(t *testing.T) {
 		t.Errorf("Should fail when header can not be parsed")
 		t.Errorf("Should fail when header can not be parsed")
 	}
 	}
 
 
-	serverHeader, err = ParseServerHeader("(bad header)")
-	if err.Error() != "Bad header: '/' missing" {
+	if serverHeader, err = ParseServerHeader("(bad header)"); err.Error() != "Bad header: '/' missing" {
 		t.Errorf("Should fail when header can not be parsed")
 		t.Errorf("Should fail when header can not be parsed")
 	}
 	}
 
 
-	serverHeader, err = ParseServerHeader("(without/spaces)")
-	if err.Error() != "Bad header: Expected single space" {
+	if serverHeader, err = ParseServerHeader("(without/spaces)"); err.Error() != "Bad header: Expected single space" {
 		t.Errorf("Should fail when header can not be parsed")
 		t.Errorf("Should fail when header can not be parsed")
 	}
 	}
 
 
-	serverHeader, err = ParseServerHeader("(header/with space)")
+	if serverHeader, err = ParseServerHeader("(header/with space)"); err != nil {
+		t.Errorf("Expected err to not exist when ParseServerHeader(\"(header/with space)\")")
+	}
+
 	if serverHeader.App != "(header" {
 	if serverHeader.App != "(header" {
 		t.Errorf("Expected serverHeader.App to equal \"(header\"")
 		t.Errorf("Expected serverHeader.App to equal \"(header\"")
 	}
 	}

+ 1 - 2
pkg/httputils/mimetype_test.go

@@ -7,8 +7,7 @@ import (
 func TestDetectContentType(t *testing.T) {
 func TestDetectContentType(t *testing.T) {
 	input := []byte("That is just a plain text")
 	input := []byte("That is just a plain text")
 
 
-	contentType, _, err := DetectContentType(input)
-	if err != nil || contentType != "text/plain" {
+	if contentType, _, err := DetectContentType(input); err != nil || contentType != "text/plain" {
 		t.Errorf("TestDetectContentType failed")
 		t.Errorf("TestDetectContentType failed")
 	}
 	}
 }
 }