-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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":"末吉"} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
|
||
} |