Skip to content

mukai 課題4提出 #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions kadai4/mukai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 実行例
```
curl -v http://localhost:8080
* Rebuilt URL to: http://localhost:8080/
* Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Date: Wed, 11 Jul 2018 16:57:05 GMT
< Content-Length: 19
<
{"data":"大吉"}

curl -v http://localhost:8080
* Rebuilt URL to: http://localhost:8080/
* Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Date: Wed, 11 Jul 2018 16:57:14 GMT
< Content-Length: 16
<
{"data":"凶"}

```

# 指定日のテスト
おみくじをひく日時を返却するメソッドを持ったインタフェースをHandlerにもたせる.

本番実装では、実行時の日地を返却するメソッドを持った構造体を渡し、テスト時には任意の日時を返却する構造体を渡すことで、実装を変えずに任意の日付のおみくじ結果を取得できる.

21 changes: 21 additions & 0 deletions kadai4/mukai/handler/dateProvider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package handler

import "time"

type TimeProvider interface {
Time() time.Time
}

type NowTimeProvider struct { }
// 実行時の日時を返却する.
func (p NowTimeProvider) Time() time.Time {
return time.Now()
}

type ArbitraryTimeProvider struct {
time time.Time
}
// ArbitraryTimeProviderに設定した任意の日時を返却する.
func (p ArbitraryTimeProvider) Time() time.Time {
return p.time
}
10 changes: 10 additions & 0 deletions kadai4/mukai/handler/middleWare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package handler

import "net/http"

func HeaderMiddleWare(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json; charset=utf-8")
h.ServeHTTP(w, r)
})
}
41 changes: 41 additions & 0 deletions kadai4/mukai/handler/omikujiHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package handler

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

type OmikujiHandler struct {
TimeProvider TimeProvider
}
func (o OmikujiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
result := result{Data: o.omikuji()}
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(result); err != nil {
fmt.Fprintln(w, "error")
}
fmt.Fprintln(w, buf.String())
}

func (o OmikujiHandler) omikuji() string {
time := o.TimeProvider.Time()
if time.Month() == 1 && (1 <= time.Day() && time.Day() <= 3) {
return "大吉"
}
i := rand.Intn(4)
switch i {
case 0: return "大吉"
case 1: return "中吉"
case 2: return "小吉"
case 3: return "凶"
default: return "凶"
}
}

type result struct {
Data string `json:"data"`
}

46 changes: 46 additions & 0 deletions kadai4/mukai/handler/omikujiHandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package handler

import (
"net/http/httptest"
"testing"
"time"
"reflect"
"encoding/json"
"log"
)

func TestOmikujiHandler(t *testing.T) {
type fields struct {
month time.Month
day int
}

tests := []struct {
name string
fields fields
want string
}{
{name:"1/1", fields:fields{month:1, day:1}, want:"大吉"},
{name:"1/2", fields:fields{month:1, day:2}, want:"大吉"},
{name:"1/3", fields:fields{month:1, day:3}, want:"大吉"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
loc, _ := time.LoadLocation("Asia/Tokyo")
date := time.Date(2018, tt.fields.month, tt.fields.day, 0, 0, 0, 0, loc)
OmikujiHandler{TimeProvider: ArbitraryTimeProvider{time: date}}.ServeHTTP(w, r)
rw := w.Result()
defer rw.Body.Close()
decoder := json.NewDecoder(rw.Body)
var result result
if err := decoder.Decode(&result); err != nil {
log.Fatal(err)
}
if !reflect.DeepEqual(result.Data, tt.want) {
t.Errorf("omikujiHandler = %v, want %v", result.Data, tt.want)
}
})
}
}
17 changes: 17 additions & 0 deletions kadai4/mukai/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"dojo2/kadai4/mukai/handler"
"math/rand"
"net/http"
"time"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

func main() {
http.Handle("/", handler.HeaderMiddleWare(handler.OmikujiHandler{TimeProvider: handler.NowTimeProvider{}}))
http.ListenAndServe(":8080", nil)
}