summaryrefslogtreecommitdiff
path: root/server/main_test.go
blob: e588ae11c7a74e1e4f43bfa67e43c0d8add7801d (plain)
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
package main

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

func TestHandleCaesar(t *testing.T) {
	bodyReader := strings.NewReader("hello world")
	req := httptest.NewRequest("POST", "/", bodyReader)
	resp := httptest.NewRecorder()

	handler := handleCaesar()
	handler(resp, req)

	if resp.Code != http.StatusOK {
		t.Errorf("expected OK status %d but got %d", http.StatusOK, resp.Code)
	}

	body := string(resp.Body.Bytes())
	if body != "khoor zruog" {
		t.Errorf("got unexpected body %s", body)
	}
}

func TestCaesarBadRequest(t *testing.T) {
	req := httptest.NewRequest("GET", "/", nil)
	resp := httptest.NewRecorder()
	handler := handleCaesar()
	handler(resp, req)
	if resp.Code != http.StatusBadRequest {
		t.Errorf("status should have been bad request, got %d", resp.Code)
	}
	if len(resp.Body.Bytes()) != 0 {
		t.Errorf("should not have gotten a response body")
	}
}