Skip to content

Instantly share code, notes, and snippets.

@iporsut
Created March 21, 2019 07:20
Show Gist options
  • Save iporsut/8286a0166943c7a8500443166b617d62 to your computer and use it in GitHub Desktop.
Save iporsut/8286a0166943c7a8500443166b617d62 to your computer and use it in GitHub Desktop.
calc http handler
package calc
import (
"fmt"
"net/http"
"strconv"
)
func Add(a int, b int) int {
return a + b
}
func Sub(a, b int) int {
return a - b
}
func Mul(a, b int) int {
return a * b
}
func Div(a, b int) int {
return a / b
}
func AddHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
a, _ := strconv.Atoi(r.FormValue("a"))
b, _ := strconv.Atoi(r.FormValue("b"))
fmt.Fprintf(w, `{"result":%d}`, Add(a, b))
}
func SubHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
a, _ := strconv.Atoi(r.FormValue("a"))
b, _ := strconv.Atoi(r.FormValue("b"))
fmt.Fprintf(w, `{"result":%d}`, Sub(a, b))
}
func MulHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
a, _ := strconv.Atoi(r.FormValue("a"))
b, _ := strconv.Atoi(r.FormValue("b"))
fmt.Fprintf(w, `{"result":%d}`, Mul(a, b))
}
func DivHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
a, _ := strconv.Atoi(r.FormValue("a"))
b, _ := strconv.Atoi(r.FormValue("b"))
fmt.Fprintf(w, `{"result":%d}`, Div(a, b))
}
func SetRouter() {
http.HandleFunc("/add/", AddHandler)
http.HandleFunc("/sub/", SubHandler)
http.HandleFunc("/mul/", MulHandler)
http.HandleFunc("/div/", DivHandler)
}
package calc
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAdd(t *testing.T) {
testCases := []struct {
a, b int
expect int
}{
{10, 0, 10},
{0, 10, 10},
{10, 20, 30},
{10, -20, -10},
{-10, 20, 10},
{-10, -20, -30},
}
for _, tc := range testCases {
tcName := fmt.Sprintf("Add(%d,%d)", tc.a, tc.b)
t.Run(tcName, func(t *testing.T) {
assert.Equal(t, tc.expect, Add(tc.a, tc.b))
})
}
}
// func TestCalc(t *testing.T) {
// t.Run("Add(10,20)", func(t *testing.T) {
// assert.Equal(t, 30, Add(10, 20))
// })
// t.Run("Sub(50,20)", func(t *testing.T) {
// assert.Equal(t, 30, Sub(50, 20))
// })
// t.Run("Mul(15,2)", func(t *testing.T) {
// assert.Equal(t, 30, Mul(15, 2))
// })
// t.Run("Div(60,2)", func(t *testing.T) {
// assert.Equal(t, 30, Div(60, 2))
// })
// }
func ExampleAdd() {
n := Add(10, 20)
fmt.Println(n)
// Output:
// 30
}
func TestAddHandler(t *testing.T) {
req := httptest.NewRequest("GET", "http://test.com/add?a=10&b=20", nil)
w := httptest.NewRecorder()
AddHandler(w, req)
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
assert.Equal(t, `{"result":30}`, string(body))
}
func TestSubHandler(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(SubHandler))
defer ts.Close()
req, _ := http.NewRequest("GET", ts.URL+"/sub?a=30&b=20", nil)
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
assert.Equal(t, `{"result":10}`, string(body))
}
func TestRouter(t *testing.T) {
SetRouter()
testCases := []struct {
name string
path string
result int
}{
{"Add", "/add?a=10&b=20", 30},
{"Sub", "/sub?a=20&b=10", 10},
{"Mul", "/mul?a=10&b=20", 200},
{"Div", "/div?a=30&b=10", 3},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
testRouter(t, tc.path, tc.result)
})
}
}
func testRouter(t *testing.T, path string, result int) {
ts := httptest.NewServer(http.DefaultServeMux)
defer ts.Close()
req, _ := http.NewRequest("GET", ts.URL+path, nil)
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
expectJSON := fmt.Sprintf(`{"result":%d}`, result)
assert.Equal(t, expectJSON, string(body))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment