Passed
Push — main ( 331b34...0c52e1 )
by Acho
01:13
created

internal/helpers/test_helper.go   A

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
dl 0
loc 26
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A helpers.MakeRequestCapturingTestServer 0 7 3
A helpers.MakeTestServer 0 6 3
1
package helpers
2
3
import (
4
	"net/http"
5
	"net/http/httptest"
6
)
7
8
// MakeTestServer creates an api server for testing
9
func MakeTestServer(responseCode int, body string) *httptest.Server {
10
	return httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
11
		res.WriteHeader(responseCode)
12
		_, err := res.Write([]byte(body))
13
		if err != nil {
14
			panic(err)
15
		}
16
	}))
17
}
18
19
// MakeRequestCapturingTestServer creates an api server that captures the request object
20
func MakeRequestCapturingTestServer(responseCode int, body string, request *http.Request) *httptest.Server {
21
	return httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
22
		*request = *req
23
		res.WriteHeader(responseCode)
24
		_, err := res.Write([]byte(body))
25
		if err != nil {
26
			panic(err)
27
		}
28
	}))
29
}
30