GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Setup Failed
Push — master ( 27c8ce...a5aa25 )
by Amir
01:18
created

httpserver.TestIndexHandler   C

Complexity

Conditions 9

Size

Total Lines 71
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 53
nop 1
dl 0
loc 71
rs 6.2048
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package httpserver
2
3
import (
4
	"github.com/arazmj/gerdu/lrucache"
5
	"github.com/gorilla/mux"
6
	"net/http"
7
	"net/http/httptest"
8
	"strings"
9
	"testing"
10
)
11
12
func TestIndexHandler(t *testing.T) {
13
	gerdu := lrucache.NewCache(2)
14
	tests := []struct {
15
		name             string
16
		r                *http.Request
17
		w                *httptest.ResponseRecorder
18
		expectedStatus   int
19
		expectedResponse string
20
	}{
21
		{
22
			name:           "Put 1:1",
23
			r:              httptest.NewRequest("PUT", "/cache/1", strings.NewReader("1")),
24
			w:              httptest.NewRecorder(),
25
			expectedStatus: http.StatusCreated,
26
		},
27
		{
28
			name:           "Put 2:2",
29
			r:              httptest.NewRequest("PUT", "/cache/2", strings.NewReader("2")),
30
			w:              httptest.NewRecorder(),
31
			expectedStatus: http.StatusCreated,
32
		},
33
		{
34
			name:           "Put 3:3",
35
			r:              httptest.NewRequest("PUT", "/cache/3", strings.NewReader("3")),
36
			w:              httptest.NewRecorder(),
37
			expectedStatus: http.StatusCreated,
38
		},
39
		{
40
			name:             "Get 2:2",
41
			r:                httptest.NewRequest("GET", "/cache/2", nil),
42
			w:                httptest.NewRecorder(),
43
			expectedResponse: "2",
44
			expectedStatus:   http.StatusOK,
45
		},
46
		{
47
			name:             "Get 3:3",
48
			r:                httptest.NewRequest("GET", "/cache/3", nil),
49
			w:                httptest.NewRecorder(),
50
			expectedResponse: "3",
51
			expectedStatus:   http.StatusOK,
52
		},
53
		{
54
			name:           "Get 1:1",
55
			r:              httptest.NewRequest("GET", "/cache/1", nil),
56
			w:              httptest.NewRecorder(),
57
			expectedStatus: http.StatusNotFound,
58
		},
59
	}
60
	for _, test := range tests {
61
		test := test
62
		t.Run(test.name, func(t *testing.T) {
63
			if strings.HasPrefix(test.name, "Put") {
64
				router := mux.NewRouter()
65
				router.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) {
66
					putHandler(w, r, gerdu, false)
67
				})
68
				router.ServeHTTP(test.w, test.r)
69
70
				if test.w.Code != test.expectedStatus {
71
					t.Errorf("Failed to produce expected status code %d, got %d", test.expectedStatus, test.w.Code)
72
				}
73
			} else {
74
				router := mux.NewRouter()
75
				router.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) {
76
					getHandler(w, r, gerdu, false)
77
				})
78
				router.ServeHTTP(test.w, test.r)
79
80
				if test.w.Code != test.expectedStatus || test.expectedResponse != test.w.Body.String() {
81
					t.Errorf("Failed to produce expected result %d, %s, got %d, %s",
82
						test.expectedStatus, test.expectedResponse, test.w.Code, test.w.Body.String())
83
				}
84
85
			}
86
		})
87
	}
88
}
89