Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
cg33 committed Aug 4, 2018
1 parent 31b975c commit ea14e04
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOTEST=$(GOCMD)test
GOGET=$(GOCMD) get
BINARY_NAME=go-admin
BINARY_UNIX=$(BINARY_NAME)_unix
Expand Down
71 changes: 71 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"testing"
"github.com/magiconair/properties/assert"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
"bufio"
"net/http"
"fmt"
"strings"
"net"
)

func TestIndexApi(t *testing.T) {

ln := fasthttputil.NewInmemoryListener()
defer ln.Close()

router := InitRouter()
go fasthttp.Serve(ln, router.Handler)

c, err := ln.Dial()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

req, _ := http.NewRequest("GET", "/", nil)
resp, _ := SendRequest(&c, req)

assert.Equal(t, 302, resp.StatusCode())
}

func SendRequest(c *net.Conn, req *http.Request) (resp fasthttp.Response, err error) {
req.Host = "127.0.0.1"

if _, err = (*c).Write([]byte(FormatRequest(req))); err != nil {
return resp, err
}
br := bufio.NewReader(*c)
if err = resp.Read(br); err != nil {
return resp, err
}
return resp, nil
}

func FormatRequest(r *http.Request) string {
// Create return string
var request []string
// Add the request string
url := fmt.Sprintf("%v %v %v", r.Method, r.URL, r.Proto)
request = append(request, url)
// Add the host
request = append(request, fmt.Sprintf("Host: %v", r.Host))
// Loop through headers
for name, headers := range r.Header {
name = strings.ToLower(name)
for _, h := range headers {
request = append(request, fmt.Sprintf("%v: %v", name, h))
}
}

// If this is a POST, add post data
if r.Method == "POST" {
r.ParseForm()
request = append(request, "\n")
request = append(request, r.Form.Encode())
}
// Return the request as a string
return strings.Join(request, "\n") + "\r\n\r\n"
}
18 changes: 18 additions & 0 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"revision": "28db72dff5a1512dab256b4b7f85c5aa8c36cf73",
"revisionTime": "2018-02-11T07:41:05Z"
},
{
"checksumSHA1": "CSPbwbyzqA6sfORicn4HFtIhF/c=",
"path": "github.com/davecgh/go-spew/spew",
"revision": "8991bc29aa16c548c550c7ff78260e27b9ab7c73",
"revisionTime": "2018-02-21T22:46:20Z"
},
{
"checksumSHA1": "31d1HRaNEiWa9GxI2Tn9aDKiNng=",
"path": "github.com/fasthttp-contrib/sessions",
Expand Down Expand Up @@ -126,12 +132,24 @@
"revision": "9520e82c474b0a04dd04f8a40959027271bab992",
"revisionTime": "2017-02-06T15:57:36Z"
},
{
"checksumSHA1": "LuFv4/jlrmFNnDb/5SCSEPAM9vU=",
"path": "github.com/pmezard/go-difflib/difflib",
"revision": "792786c7400a136282c1664665ae0a8db921c6c2",
"revisionTime": "2016-01-10T10:55:54Z"
},
{
"checksumSHA1": "S1hwXXk0F7KHj9xRH0vkRwFgzTM=",
"path": "github.com/shiyanhui/hero",
"revision": "f4b770cdc44f6f47a7a1db3431cea6338220c9cb",
"revisionTime": "2018-04-05T01:48:29Z"
},
{
"checksumSHA1": "c6pbpF7eowwO59phRTpF8cQ80Z0=",
"path": "github.com/stretchr/testify/assert",
"revision": "f35b8ab0b5a2cef36673838d662e249dd9c94686",
"revisionTime": "2018-05-06T18:05:49Z"
},
{
"checksumSHA1": "LTOa3BADhwvT0wFCknPueQALm8I=",
"path": "github.com/valyala/bytebufferpool",
Expand Down

0 comments on commit ea14e04

Please sign in to comment.