Skip to content

Commit d5b5e86

Browse files
committed
kadai4-nKumaya
1 parent 1153a9d commit d5b5e86

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

kadai4/nKumaya/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 課題4 おみくじAPIを作ってみよう
2+
3+
JSON形式でおみくじの結果を返す
4+
正月(1/1-1/3)だけ大吉にする
5+
ハンドラのテストを書いてみる
6+
7+
## 実行方法
8+
```
9+
go run main.go
10+
curl http://localhost:8080
11+
{"date":"2018-09-18T14:00:25.062023813+09:00","result":"末吉"}
12+
```

kadai4/nKumaya/main.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
"math/rand"
9+
"net/http"
10+
"time"
11+
)
12+
13+
var fortunes = []string{"大吉", "吉", "中吉", "小吉", "末吉", "凶", "大凶"}
14+
15+
type Omikuji struct {
16+
Date time.Time `json:"date"`
17+
Result string `json:"result"`
18+
}
19+
20+
func NewOmikuji(date time.Time) *Omikuji {
21+
o := Omikuji{Date: date}
22+
return &o
23+
}
24+
25+
type Adapter func(http.Handler) http.Handler
26+
27+
func Adapt(h http.Handler, adapters ...Adapter) http.Handler {
28+
for _, adapter := range adapters {
29+
h = adapter(h)
30+
}
31+
return h
32+
}
33+
34+
func SetHeader() Adapter {
35+
return func(h http.Handler) http.Handler {
36+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
37+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
38+
h.ServeHTTP(w, r)
39+
})
40+
}
41+
}
42+
43+
func (o *Omikuji) handle(w http.ResponseWriter, r *http.Request) {
44+
if o.Date.Month() == 1 && (o.Date.Day() >= 1 && o.Date.Day() <= 3) {
45+
o.Result = "大吉"
46+
} else {
47+
o.Result = fortunes[rand.Int()%len(fortunes)]
48+
}
49+
var buf bytes.Buffer
50+
enc := json.NewEncoder(&buf)
51+
if err := enc.Encode(o); err != nil {
52+
log.Fatal(err)
53+
}
54+
fmt.Fprintf(w, buf.String())
55+
}
56+
57+
func main() {
58+
o := NewOmikuji(time.Now())
59+
handler := http.HandlerFunc(o.handle)
60+
http.Handle("/", Adapt(handler, SetHeader()))
61+
http.ListenAndServe(":8080", nil)
62+
}

kadai4/nKumaya/main_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"net/http/httptest"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestHandler(t *testing.T) {
10+
t.Helper()
11+
dateSet := []time.Time{
12+
time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local),
13+
time.Date(2019, 1, 3, 23, 59, 59, 0, time.Local),
14+
time.Now(),
15+
}
16+
omikujis := make([]*Omikuji, 3)
17+
for i, v := range dateSet {
18+
omikujis[i] = NewOmikuji(v)
19+
}
20+
w := httptest.NewRecorder()
21+
r := httptest.NewRequest("GET", "/", nil)
22+
N := 100
23+
daikichiCount := 0
24+
t.Run("1/1 test", func(t *testing.T) {
25+
for i := 0; i < N; i++ {
26+
omikujis[0].handle(w, r)
27+
if omikujis[0].Result == "大吉" {
28+
daikichiCount++
29+
}
30+
}
31+
if daikichiCount != N {
32+
t.Errorf("1/1 daikichiCount: %d\n", daikichiCount)
33+
}
34+
daikichiCount = 0
35+
})
36+
37+
t.Run("1/3 test", func(t *testing.T) {
38+
for i := 0; i < N; i++ {
39+
omikujis[1].handle(w, r)
40+
if omikujis[1].Result == "大吉" {
41+
daikichiCount++
42+
}
43+
}
44+
if daikichiCount != N {
45+
t.Errorf("1/3 daikichiCount: %d\n", daikichiCount)
46+
}
47+
daikichiCount = 0
48+
})
49+
50+
t.Run("normal day test", func(t *testing.T) {
51+
for i := 0; i < N; i++ {
52+
omikujis[2].handle(w, r)
53+
if omikujis[2].Result == "大吉" {
54+
daikichiCount++
55+
}
56+
}
57+
if daikichiCount == N {
58+
t.Errorf("daikichiCount: %d\n", daikichiCount)
59+
}
60+
})
61+
62+
}

0 commit comments

Comments
 (0)