Selaa lähdekoodia

add initial test for main and index route

master
John Robinson 4 vuotta sitten
vanhempi
commit
3f4f573dc6
No known key found for this signature in database
4 muutettua tiedostoa jossa 49 lisäystä ja 6 poistoa
  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 Näytä tiedosto

@@ -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 Näytä tiedosto

@@ -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 Näytä tiedosto

@@ -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 Näytä tiedosto

@@ -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")
}

Loading…
Peruuta
Tallenna