Skip to content
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

kadai4 intial commit #69

Open
wants to merge 1 commit 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
51 changes: 51 additions & 0 deletions kadai4/chokkoyamada/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
91 changes: 91 additions & 0 deletions kadai4/chokkoyamada/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}