forked from GoAdminGroup/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cg33
committed
Aug 4, 2018
1 parent
31b975c
commit ea14e04
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters