Skip to content

Commit

Permalink
kadai4-nKumaya
Browse files Browse the repository at this point in the history
  • Loading branch information
nKumaya committed Sep 18, 2018
1 parent 1153a9d commit d5b5e86
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
12 changes: 12 additions & 0 deletions kadai4/nKumaya/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 課題4 おみくじAPIを作ってみよう

JSON形式でおみくじの結果を返す
正月(1/1-1/3)だけ大吉にする
ハンドラのテストを書いてみる

## 実行方法
```
go run main.go
curl http://localhost:8080
{"date":"2018-09-18T14:00:25.062023813+09:00","result":"末吉"}
```
62 changes: 62 additions & 0 deletions kadai4/nKumaya/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"time"
)

var fortunes = []string{"大吉", "吉", "中吉", "小吉", "末吉", "凶", "大凶"}

type Omikuji struct {
Date time.Time `json:"date"`
Result string `json:"result"`
}

func NewOmikuji(date time.Time) *Omikuji {
o := Omikuji{Date: date}
return &o
}

type Adapter func(http.Handler) http.Handler

func Adapt(h http.Handler, adapters ...Adapter) http.Handler {
for _, adapter := range adapters {
h = adapter(h)
}
return h
}

func SetHeader() Adapter {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
h.ServeHTTP(w, r)
})
}
}

func (o *Omikuji) handle(w http.ResponseWriter, r *http.Request) {
if o.Date.Month() == 1 && (o.Date.Day() >= 1 && o.Date.Day() <= 3) {
o.Result = "大吉"
} else {
o.Result = fortunes[rand.Int()%len(fortunes)]
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
if err := enc.Encode(o); err != nil {
log.Fatal(err)
}
fmt.Fprintf(w, buf.String())
}

func main() {
o := NewOmikuji(time.Now())
handler := http.HandlerFunc(o.handle)
http.Handle("/", Adapt(handler, SetHeader()))
http.ListenAndServe(":8080", nil)
}
62 changes: 62 additions & 0 deletions kadai4/nKumaya/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

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

func TestHandler(t *testing.T) {
t.Helper()
dateSet := []time.Time{
time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local),
time.Date(2019, 1, 3, 23, 59, 59, 0, time.Local),
time.Now(),
}
omikujis := make([]*Omikuji, 3)
for i, v := range dateSet {
omikujis[i] = NewOmikuji(v)
}
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
N := 100
daikichiCount := 0
t.Run("1/1 test", func(t *testing.T) {
for i := 0; i < N; i++ {
omikujis[0].handle(w, r)
if omikujis[0].Result == "大吉" {
daikichiCount++
}
}
if daikichiCount != N {
t.Errorf("1/1 daikichiCount: %d\n", daikichiCount)
}
daikichiCount = 0
})

t.Run("1/3 test", func(t *testing.T) {
for i := 0; i < N; i++ {
omikujis[1].handle(w, r)
if omikujis[1].Result == "大吉" {
daikichiCount++
}
}
if daikichiCount != N {
t.Errorf("1/3 daikichiCount: %d\n", daikichiCount)
}
daikichiCount = 0
})

t.Run("normal day test", func(t *testing.T) {
for i := 0; i < N; i++ {
omikujis[2].handle(w, r)
if omikujis[2].Result == "大吉" {
daikichiCount++
}
}
if daikichiCount == N {
t.Errorf("daikichiCount: %d\n", daikichiCount)
}
})

}

0 comments on commit d5b5e86

Please sign in to comment.