test: use MustNewRequest.

This commit is contained in:
Fernandez Ludovic 2017-06-03 14:58:35 +02:00 committed by Ludovic Fernandez
parent 2223587fc0
commit a1a0420314
7 changed files with 27 additions and 37 deletions

View file

@ -9,6 +9,7 @@ import (
"testing"
"github.com/codegangsta/negroni"
"github.com/containous/traefik/testhelpers"
"github.com/containous/traefik/types"
"github.com/stretchr/testify/assert"
)
@ -88,7 +89,7 @@ func TestBasicAuthFail(t *testing.T) {
defer ts.Close()
client := &http.Client{}
req, err := http.NewRequest("GET", ts.URL, nil)
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
req.SetBasicAuth("test", "test")
res, err := client.Do(req)
assert.NoError(t, err, "there should be no error")
@ -112,7 +113,7 @@ func TestBasicAuthSuccess(t *testing.T) {
defer ts.Close()
client := &http.Client{}
req, err := http.NewRequest("GET", ts.URL, nil)
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
req.SetBasicAuth("test", "test")
res, err := client.Do(req)
assert.NoError(t, err, "there should be no error")
@ -148,7 +149,7 @@ func TestDigestAuthFail(t *testing.T) {
defer ts.Close()
client := &http.Client{}
req, err := http.NewRequest("GET", ts.URL, nil)
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
req.SetBasicAuth("test", "test")
res, err := client.Do(req)
assert.NoError(t, err, "there should be no error")
@ -174,10 +175,11 @@ func TestBasicAuthUserHeader(t *testing.T) {
defer ts.Close()
client := &http.Client{}
req, err := http.NewRequest("GET", ts.URL, nil)
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
req.SetBasicAuth("test", "test")
res, err := client.Do(req)
assert.NoError(t, err, "there should be no error")
assert.Equal(t, http.StatusOK, res.StatusCode, "they should be equal")
body, err := ioutil.ReadAll(res.Body)

View file

@ -8,6 +8,7 @@ import (
"testing"
"github.com/codegangsta/negroni"
"github.com/containous/traefik/testhelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -281,8 +282,7 @@ func TestIPWhitelisterHandle(t *testing.T) {
n.UseHandler(handler)
for _, testIP := range test.passIPs {
req, err := http.NewRequest("GET", "/", nil)
require.NoError(t, err)
req := testhelpers.MustNewRequest(http.MethodGet, "/", nil)
req.RemoteAddr = testIP + ":2342"
recorder := httptest.NewRecorder()
@ -293,8 +293,7 @@ func TestIPWhitelisterHandle(t *testing.T) {
}
for _, testIP := range test.rejectIPs {
req, err := http.NewRequest("GET", "/", nil)
require.NoError(t, err)
req := testhelpers.MustNewRequest(http.MethodGet, "/", nil)
req.RemoteAddr = testIP + ":2342"
recorder := httptest.NewRecorder()

View file

@ -2,7 +2,6 @@ package middlewares
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
@ -10,6 +9,7 @@ import (
"testing"
"github.com/codegangsta/negroni"
"github.com/containous/traefik/testhelpers"
"github.com/containous/traefik/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
@ -28,8 +28,8 @@ func TestPrometheus(t *testing.T) {
recorder := httptest.NewRecorder()
req1 := mustNewRequest("GET", "http://localhost:3000/ok", ioutil.NopCloser(nil))
req2 := mustNewRequest("GET", "http://localhost:3000/metrics", ioutil.NopCloser(nil))
req1 := testhelpers.MustNewRequest(http.MethodGet, "http://localhost:3000/ok", ioutil.NopCloser(nil))
req2 := testhelpers.MustNewRequest(http.MethodGet, "http://localhost:3000/metrics", ioutil.NopCloser(nil))
httpHandler := setupTestHTTPHandler()
httpHandler.ServeHTTP(recorder, req1)
@ -60,7 +60,7 @@ func TestPrometheus(t *testing.T) {
name: reqsTotalName,
labels: map[string]string{
"code": "200",
"method": "GET",
"method": http.MethodGet,
"service": "test",
},
assert: func(family *dto.MetricFamily) {
@ -99,7 +99,7 @@ func TestPrometheus(t *testing.T) {
},
}
assert.Equal(t, len(tests), len(metricsFamilies)-initialMetricsFamilyCount, "gathered traefic metrics count does not match tests count")
assert.Equal(t, len(tests), len(metricsFamilies)-initialMetricsFamilyCount, "gathered Traefik metrics count does not match tests count")
for _, test := range tests {
family := findMetricFamily(test.name, metricsFamilies)
@ -123,7 +123,7 @@ func TestPrometheusRegisterMetricsMultipleTimes(t *testing.T) {
defer resetPrometheusValues()
recorder := httptest.NewRecorder()
req1 := mustNewRequest("GET", "http://localhost:3000/ok", ioutil.NopCloser(nil))
req1 := testhelpers.MustNewRequest(http.MethodGet, "http://localhost:3000/ok", ioutil.NopCloser(nil))
httpHandler := setupTestHTTPHandler()
httpHandler.ServeHTTP(recorder, req1)
@ -162,15 +162,6 @@ func setupTestHTTPHandler() http.Handler {
return n
}
// mustNewRequest is like http.NewRequest but panics if an error occurs.
func mustNewRequest(method, urlStr string, body io.Reader) *http.Request {
req, err := http.NewRequest(method, urlStr, body)
if err != nil {
panic(fmt.Sprintf("NewRequest(%s, %s, %+v): %s", method, urlStr, body, err))
}
return req
}
func resetPrometheusValues() {
_, collectors := newPrometheusMetrics()

View file

@ -4,8 +4,8 @@ import (
"net/http"
"testing"
"github.com/containous/traefik/testhelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReplacePath(t *testing.T) {
@ -28,8 +28,7 @@ func TestReplacePath(t *testing.T) {
}),
}
req, err := http.NewRequest(http.MethodGet, "http://localhost"+path, nil)
require.NoError(t, err, "%s: unexpected error.", path)
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost"+path, nil)
handler.ServeHTTP(nil, req)
assert.Equal(t, expectedPath, replacementPath, "Unexpected path.")

View file

@ -5,8 +5,8 @@ import (
"net/http/httptest"
"testing"
"github.com/containous/traefik/testhelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStripPrefixRegex(t *testing.T) {
@ -75,10 +75,9 @@ func TestStripPrefixRegex(t *testing.T) {
})
handler := NewStripPrefixRegex(handlerPath, testPrefixRegex)
req, err := http.NewRequest(http.MethodGet, "http://localhost"+test.path, nil)
require.NoError(t, err, "%s: unexpected error.", test.path)
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost"+test.path, nil)
resp := &httptest.ResponseRecorder{Code: http.StatusOK}
handler.ServeHTTP(resp, req)
assert.Equal(t, test.expectedStatusCode, resp.Code, "Unexpected status code.")

View file

@ -5,8 +5,8 @@ import (
"net/http/httptest"
"testing"
"github.com/containous/traefik/testhelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStripPrefix(t *testing.T) {
@ -102,10 +102,9 @@ func TestStripPrefix(t *testing.T) {
}),
}
req, err := http.NewRequest(http.MethodGet, "http://localhost"+test.path, nil)
require.NoError(t, err, "%s: unexpected error.", test.desc)
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost"+test.path, nil)
resp := &httptest.ResponseRecorder{Code: http.StatusOK}
handler.ServeHTTP(resp, req)
assert.Equal(t, test.expectedStatusCode, resp.Code, "Unexpected status code.")

View file

@ -6,6 +6,7 @@ import (
"testing"
"github.com/containous/mux"
"github.com/containous/traefik/testhelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -20,7 +21,7 @@ func TestParseOneRule(t *testing.T) {
routeResult, err := rules.Parse(expression)
require.NoError(t, err, "Error while building route for %s", expression)
request, err := http.NewRequest("GET", "http://foo.bar", nil)
request := testhelpers.MustNewRequest(http.MethodGet, "http://foo.bar", nil)
routeMatch := routeResult.Match(request, &mux.RouteMatch{Route: routeResult})
assert.True(t, routeMatch, "Rule %s don't match.", expression)
@ -37,12 +38,12 @@ func TestParseTwoRules(t *testing.T) {
require.NoError(t, err, "Error while building route for %s.", expression)
request, _ := http.NewRequest("GET", "http://foo.bar/foobar", nil)
request := testhelpers.MustNewRequest(http.MethodGet, "http://foo.bar/foobar", nil)
routeMatch := routeResult.Match(request, &mux.RouteMatch{Route: routeResult})
assert.False(t, routeMatch, "Rule %s don't match.", expression)
request, _ = http.NewRequest("GET", "http://foo.bar/FOObar", nil)
request = testhelpers.MustNewRequest(http.MethodGet, "http://foo.bar/FOObar", nil)
routeMatch = routeResult.Match(request, &mux.RouteMatch{Route: routeResult})
assert.True(t, routeMatch, "Rule %s don't match.", expression)