Browse code

use httpError in a separate test

Victor Vieux authored on 2013/10/17 04:23:47
Showing 1 changed files
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"bufio"
6 6
 	"bytes"
7 7
 	"encoding/json"
8
+	"fmt"
8 9
 	"github.com/dotcloud/docker/utils"
9 10
 	"io"
10 11
 	"net"
... ...
@@ -12,6 +13,7 @@ import (
12 12
 	"net/http/httptest"
13 13
 	"os"
14 14
 	"path"
15
+	"strings"
15 16
 	"testing"
16 17
 	"time"
17 18
 )
... ...
@@ -40,6 +42,25 @@ func TestGetBoolParam(t *testing.T) {
40 40
 	}
41 41
 }
42 42
 
43
+func TesthttpError(t *testing.T) {
44
+	r := httptest.NewRecorder()
45
+
46
+	httpError(r, fmt.Errorf("No such method"))
47
+	if r.Code != http.StatusNotFound {
48
+		t.Fatalf("Expected %d, got %d", http.StatusNotFound, r.Code)
49
+	}
50
+
51
+	httpError(r, fmt.Errorf("This accound hasn't been activated"))
52
+	if r.Code != http.StatusForbidden {
53
+		t.Fatalf("Expected %d, got %d", http.StatusForbidden, r.Code)
54
+	}
55
+
56
+	httpError(r, fmt.Errorf("Some error"))
57
+	if r.Code != http.StatusInternalServerError {
58
+		t.Fatalf("Expected %d, got %d", http.StatusInternalServerError, r.Code)
59
+	}
60
+}
61
+
43 62
 func TestGetVersion(t *testing.T) {
44 63
 	var err error
45 64
 	runtime := mkRuntime(t)
... ...
@@ -243,7 +264,11 @@ func TestGetImagesJSON(t *testing.T) {
243 243
 		t.Fatalf("Error expected, received none")
244 244
 	}
245 245
 
246
-	httpError(r4, err)
246
+	if !strings.HasPrefix(err.Error(), "Bad parameter") {
247
+		t.Fatalf("Error should starts with \"Bad parameter\"")
248
+	}
249
+	http.Error(r4, err.Error(), http.StatusBadRequest)
250
+
247 251
 	if r4.Code != http.StatusBadRequest {
248 252
 		t.Fatalf("%d Bad Request expected, received %d\n", http.StatusBadRequest, r4.Code)
249 253
 	}