aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/http_test.go
blob: f4afd52167cb9171803b8047dd46a5249df29483 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package rpc

import (
    "net/http"
    "net/http/httptest"
    "strings"
    "testing"
)

func TestHTTPErrorResponseWithDelete(t *testing.T) {
    httpErrorResponseTest(t, "DELETE", contentType, "", http.StatusMethodNotAllowed)
}

func TestHTTPErrorResponseWithPut(t *testing.T) {
    httpErrorResponseTest(t, "PUT", contentType, "", http.StatusMethodNotAllowed)
}

func TestHTTPErrorResponseWithMaxContentLength(t *testing.T) {
    body := make([]rune, maxHTTPRequestContentLength+1, maxHTTPRequestContentLength+1)
    httpErrorResponseTest(t,
        "POST", contentType, string(body), http.StatusRequestEntityTooLarge)
}

func TestHTTPErrorResponseWithEmptyContentType(t *testing.T) {
    httpErrorResponseTest(t, "POST", "", "", http.StatusUnsupportedMediaType)
}

func TestHTTPErrorResponseWithValidRequest(t *testing.T) {
    httpErrorResponseTest(t, "POST", contentType, "", 0)
}

func httpErrorResponseTest(t *testing.T,
    method, contentType, body string, expectedResponse int) {

    request := httptest.NewRequest(method, "http://url.com", strings.NewReader(body))
    request.Header.Set("content-type", contentType)
    if response, _ := httpErrorResponse(request); response != expectedResponse {
        t.Fatalf("response code should be %d not %d", expectedResponse, response)
    }
}