|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io/ioutil" |
| 6 | + "math/rand" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestMain_handler_StatusCode(t *testing.T) { |
| 13 | + w := httptest.NewRecorder() |
| 14 | + req := httptest.NewRequest("GET", "/", nil) |
| 15 | + handler(w, req) |
| 16 | + rw := w.Result() |
| 17 | + defer rw.Body.Close() |
| 18 | + |
| 19 | + expected := http.StatusOK |
| 20 | + actual := rw.StatusCode |
| 21 | + if actual != expected { |
| 22 | + t.Errorf(`unexpected status code: expected: "%d" actual: "%d"`, expected, actual) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestMain_handler_ResponseBody(t *testing.T) { |
| 27 | + cases := map[string]struct { |
| 28 | + seed int64 |
| 29 | + nameParam string |
| 30 | + expected string |
| 31 | + }{ |
| 32 | + "KYOU": {seed: 0, nameParam: "", expected: "{\"name\":\"Gopher\",\"fortune\":\"凶\"}\n"}, |
| 33 | + "DAIKYOU": {seed: 1, nameParam: "", expected: "{\"name\":\"Gopher\",\"fortune\":\"大凶\"}\n"}, |
| 34 | + "SUEKICHI": {seed: 2, nameParam: "", expected: "{\"name\":\"Gopher\",\"fortune\":\"末吉\"}\n"}, |
| 35 | + "KICHI": {seed: 3, nameParam: "", expected: "{\"name\":\"Gopher\",\"fortune\":\"吉\"}\n"}, |
| 36 | + "CHUKICHI": {seed: 4, nameParam: "", expected: "{\"name\":\"Gopher\",\"fortune\":\"中吉\"}\n"}, |
| 37 | + "SHOKICHI": {seed: 5, nameParam: "", expected: "{\"name\":\"Gopher\",\"fortune\":\"小吉\"}\n"}, |
| 38 | + "DAICHIKI": {seed: 9, nameParam: "", expected: "{\"name\":\"Gopher\",\"fortune\":\"大吉\"}\n"}, |
| 39 | + "name param": {seed: 9, nameParam: "hioki-daichi", expected: "{\"name\":\"hioki-daichi\",\"fortune\":\"大吉\"}\n"}, |
| 40 | + } |
| 41 | + |
| 42 | + for n, c := range cases { |
| 43 | + c := c |
| 44 | + t.Run(n, func(t *testing.T) { |
| 45 | + rand.Seed(c.seed) |
| 46 | + |
| 47 | + w := httptest.NewRecorder() |
| 48 | + req := httptest.NewRequest("GET", "/", nil) |
| 49 | + |
| 50 | + if c.nameParam != "" { |
| 51 | + q := req.URL.Query() |
| 52 | + q.Add("name", c.nameParam) |
| 53 | + req.URL.RawQuery = q.Encode() |
| 54 | + } |
| 55 | + |
| 56 | + handler(w, req) |
| 57 | + rw := w.Result() |
| 58 | + defer rw.Body.Close() |
| 59 | + |
| 60 | + b, err := ioutil.ReadAll(rw.Body) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("err %s", err) |
| 63 | + } |
| 64 | + |
| 65 | + expected := c.expected |
| 66 | + actual := string(b) |
| 67 | + if actual != expected { |
| 68 | + t.Errorf(`unexpected response body: expected: "%s" actual: "%s"`, expected, actual) |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestMain_handler_DuringTheNewYear(t *testing.T) { |
| 75 | + isDuringTheNewYearFunc = func() bool { |
| 76 | + return true |
| 77 | + } |
| 78 | + |
| 79 | + w := httptest.NewRecorder() |
| 80 | + req := httptest.NewRequest("GET", "/", nil) |
| 81 | + handler(w, req) |
| 82 | + rw := w.Result() |
| 83 | + defer rw.Body.Close() |
| 84 | + |
| 85 | + b, err := ioutil.ReadAll(rw.Body) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("err %s", err) |
| 88 | + } |
| 89 | + |
| 90 | + expected := "{\"name\":\"Gopher\",\"fortune\":\"大吉\"}\n" |
| 91 | + actual := string(b) |
| 92 | + if actual != expected { |
| 93 | + t.Errorf(`unexpected response body: expected: "%s" actual: "%s"`, expected, actual) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestMain_handler_ValidationError(t *testing.T) { |
| 98 | + w := httptest.NewRecorder() |
| 99 | + req := httptest.NewRequest("GET", "/", nil) |
| 100 | + q := req.URL.Query() |
| 101 | + q.Add("name", "123456789012345678901234567890123") |
| 102 | + req.URL.RawQuery = q.Encode() |
| 103 | + handler(w, req) |
| 104 | + rw := w.Result() |
| 105 | + defer rw.Body.Close() |
| 106 | + |
| 107 | + b, err := ioutil.ReadAll(rw.Body) |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("err %s", err) |
| 110 | + } |
| 111 | + |
| 112 | + if rw.StatusCode != http.StatusBadRequest { |
| 113 | + t.Errorf(`unexpected status code: expected: %d actual: %d`, http.StatusBadRequest, rw.StatusCode) |
| 114 | + } |
| 115 | + |
| 116 | + expected := "{\"errors\":[\"Name is too long (maximum is 32 characters)\"]}\n" |
| 117 | + actual := string(b) |
| 118 | + if actual != expected { |
| 119 | + t.Errorf(`unexpected response body: expected: "%s" actual: "%s"`, expected, actual) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestMain_handler_ToJSONError(t *testing.T) { |
| 124 | + toJSONFunc = func(v interface{}) (string, error) { |
| 125 | + return "", errors.New("error in TestMain_handler_ToJSONError") |
| 126 | + } |
| 127 | + |
| 128 | + w := httptest.NewRecorder() |
| 129 | + req := httptest.NewRequest("GET", "/", nil) |
| 130 | + handler(w, req) |
| 131 | + rw := w.Result() |
| 132 | + defer rw.Body.Close() |
| 133 | + |
| 134 | + b, err := ioutil.ReadAll(rw.Body) |
| 135 | + if err != nil { |
| 136 | + t.Fatalf("err %s", err) |
| 137 | + } |
| 138 | + |
| 139 | + expected := "Internal Server Error\n" |
| 140 | + actual := string(b) |
| 141 | + if actual != expected { |
| 142 | + t.Errorf(`unexpected response body: expected: "%s" actual: "%s"`, expected, actual) |
| 143 | + } |
| 144 | +} |
0 commit comments