Browse code

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 authored on 2015/07/02 00:42:13
Showing 2 changed files
... ...
@@ -12,8 +12,7 @@ func TestDownload(t *testing.T) {
12 12
 	}
13 13
 
14 14
 	// Expected status code = 404
15
-	_, err = Download("http://docker.com/abc1234567")
16
-	if err == nil {
15
+	if _, err = Download("http://docker.com/abc1234567"); err == nil {
17 16
 		t.Errorf("Expected error to exist when Download(http://docker.com/abc1234567)")
18 17
 	}
19 18
 }
... ...
@@ -21,9 +20,7 @@ func TestDownload(t *testing.T) {
21 21
 func TestNewHTTPRequestError(t *testing.T) {
22 22
 	errorMessage := "Some error message"
23 23
 	httpResponse, _ := Download("http://docker.com")
24
-	err := NewHTTPRequestError(errorMessage, httpResponse)
25
-
26
-	if err.Error() != errorMessage {
24
+	if err := NewHTTPRequestError(errorMessage, httpResponse); err.Error() != errorMessage {
27 25
 		t.Errorf("Expected err to equal error Message")
28 26
 	}
29 27
 }
... ...
@@ -34,17 +31,18 @@ func TestParseServerHeader(t *testing.T) {
34 34
 		t.Errorf("Should fail when header can not be parsed")
35 35
 	}
36 36
 
37
-	serverHeader, err = ParseServerHeader("(bad header)")
38
-	if err.Error() != "Bad header: '/' missing" {
37
+	if serverHeader, err = ParseServerHeader("(bad header)"); err.Error() != "Bad header: '/' missing" {
39 38
 		t.Errorf("Should fail when header can not be parsed")
40 39
 	}
41 40
 
42
-	serverHeader, err = ParseServerHeader("(without/spaces)")
43
-	if err.Error() != "Bad header: Expected single space" {
41
+	if serverHeader, err = ParseServerHeader("(without/spaces)"); err.Error() != "Bad header: Expected single space" {
44 42
 		t.Errorf("Should fail when header can not be parsed")
45 43
 	}
46 44
 
47
-	serverHeader, err = ParseServerHeader("(header/with space)")
45
+	if serverHeader, err = ParseServerHeader("(header/with space)"); err != nil {
46
+		t.Errorf("Expected err to not exist when ParseServerHeader(\"(header/with space)\")")
47
+	}
48
+
48 49
 	if serverHeader.App != "(header" {
49 50
 		t.Errorf("Expected serverHeader.App to equal \"(header\"")
50 51
 	}
... ...
@@ -7,8 +7,7 @@ import (
7 7
 func TestDetectContentType(t *testing.T) {
8 8
 	input := []byte("That is just a plain text")
9 9
 
10
-	contentType, _, err := DetectContentType(input)
11
-	if err != nil || contentType != "text/plain" {
10
+	if contentType, _, err := DetectContentType(input); err != nil || contentType != "text/plain" {
12 11
 		t.Errorf("TestDetectContentType failed")
13 12
 	}
14 13
 }