From 144ba041a52815e963930d24a46836da3a3da79a Mon Sep 17 00:00:00 2001 From: chokkoyamada Date: Sun, 16 Sep 2018 12:49:32 +0900 Subject: [PATCH] kadai4 intial commit --- kadai4/chokkoyamada/main.go | 51 ++++++++++++++++++ kadai4/chokkoyamada/main_test.go | 91 ++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 kadai4/chokkoyamada/main.go create mode 100644 kadai4/chokkoyamada/main_test.go diff --git a/kadai4/chokkoyamada/main.go b/kadai4/chokkoyamada/main.go new file mode 100644 index 0000000..978c9b3 --- /dev/null +++ b/kadai4/chokkoyamada/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "net/http" + "time" + "math/rand" + "encoding/json" + "log" +) + +var timeNowFunc = time.Now +var index = func() int { + return rand.Intn(6) +} + +type Result struct { + Omikuji string `json:"omikuji"` + Time string `json:"time"` +} + +func Handler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + + var result string + omikuji := []string{"大吉", "中吉", "小吉", "吉", "凶", "大凶"} + index := index() + now := timeNowFunc() + + _, m, d := now.Date() + if m.String() == "January" && (d == 1 || d == 2 || d == 3) { + index = 0 + } + + result = omikuji[index] + + v := Result{ + Omikuji: result, + Time: timeNowFunc().Format("2006-01-02 15:04:05"), + } + + if err := json.NewEncoder(w).Encode(v); err != nil { + log.Println("Error", err) + } +} + +func main() { + rand.Seed(time.Now().UnixNano()) + + http.HandleFunc("/", Handler) + http.ListenAndServe(":8080", nil) +} diff --git a/kadai4/chokkoyamada/main_test.go b/kadai4/chokkoyamada/main_test.go new file mode 100644 index 0000000..f515bce --- /dev/null +++ b/kadai4/chokkoyamada/main_test.go @@ -0,0 +1,91 @@ +package main + +import ( + "testing" + "net/http/httptest" + "net/http" + "io/ioutil" + "encoding/json" + "time" + "math/rand" +) + +func setIndex() { + index = func() int { return 1 } +} + +func setNewYearTime() { + timeNowFunc = func() time.Time { + t, _ := time.Parse("2006-01-02", "2018-01-01") + return t + } +} + +func setIndexBack() { + index = func() int { + return rand.Intn(6) + } +} + +func setOtherTime() { + timeNowFunc = func() time.Time { + t, _ := time.Parse("2006-01-02", "2018-01-04") + return t + } +} + +func TestMainNormal(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + setIndex() + setOtherTime() + + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/", nil) + Handler(w, r) + rw := w.Result() + defer rw.Body.Close() + + if rw.StatusCode != http.StatusOK { + t.Fatal("unexpected status code") + } + b, err := ioutil.ReadAll(rw.Body) + if err != nil { + t.Fatal("unexpected error") + } + const expected = "中吉" + res := new(Result) + if err := json.Unmarshal(b, res); err != nil { + t.Fatal("json unmarshall error") + } + if res.Omikuji != expected { + t.Fatalf("result different: %v, %v", res.Omikuji, res.Time) + } +} + +func TestMainNewYear(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + setIndexBack() + setNewYearTime() + + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/", nil) + Handler(w, r) + rw := w.Result() + defer rw.Body.Close() + + if rw.StatusCode != http.StatusOK { + t.Fatal("unexpected status code") + } + b, err := ioutil.ReadAll(rw.Body) + if err != nil { + t.Fatal("unexpected error") + } + const expected = "大吉" + res := new(Result) + if err := json.Unmarshal(b, res); err != nil { + t.Fatal("json unmarshal error") + } + if res.Omikuji != expected { + t.Fatalf("result different: %v, %v", res.Omikuji, res.Time) + } +}