浏览代码

add initial test for main and index route

master
John Robinson 4 年前
父节点
当前提交
3f4f573dc6
找不到此签名对应的密钥
共有 4 个文件被更改,包括 49 次插入6 次删除
  1. +13
    -0
      endpoints.go
  2. +2
    -0
      go.mod
  3. +16
    -6
      main.go
  4. +18
    -0
      main_test.go

+ 13
- 0
endpoints.go 查看文件

@@ -0,0 +1,13 @@
package main

import (
"io"
"net/http"

_ "git.thevis.us/skepto/env"
_ "git.thevis.us/skepto/rex"
)

func Index(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "pehpeh-api")
}

+ 2
- 0
go.mod 查看文件

@@ -5,4 +5,6 @@ go 1.14
require (
git.thevis.us/skepto/env v0.0.0-20200712040037-f8a5c3d1b0fa
git.thevis.us/skepto/rex v0.10.0
github.com/gorilla/mux v1.7.4
github.com/stretchr/testify v1.2.2
)

+ 16
- 6
main.go 查看文件

@@ -1,19 +1,29 @@
package main

import (
"io"
"net/http"

"git.thevis.us/skepto/env"
"git.thevis.us/skepto/rex"
)

type Server interface {
Run()

Get(pattern string, handler interface{})

ServeHTTP(w http.ResponseWriter, r *http.Request)
}

func main() {
srv := Configure()
srv.Run()
}

func Configure() Server {
env.Set("PORT", 7777)

app := rex.New()
app.Get("/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "pehpeh-api")
})
app.Run()
server := rex.New()
server.Get("/", Index)
return server
}

+ 18
- 0
main_test.go 查看文件

@@ -0,0 +1,18 @@
package main

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestConfigure(t *testing.T) {
srv := Configure()
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()
srv.ServeHTTP(response, request)
assert.Equal(t, "pehpeh-api", response.Body.String(), "response body is expected")
assert.Equal(t, 200, response.Code, "OK response is expected")
}

正在加载...
取消
保存