本文整理汇总了Golang中github.com/valyala/fasthttp.Response.Read方法的典型用法代码示例。如果您正苦于以下问题:Golang Response.Read方法的具体用法?Golang Response.Read怎么用?Golang Response.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/valyala/fasthttp.Response
的用法示例。
在下文中一共展示了Response.Read方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestRouterServeFiles
func TestRouterServeFiles(t *testing.T) {
router := New()
recv := catchPanic(func() {
router.ServeFiles("/noFilepath", os.TempDir())
})
if recv == nil {
t.Fatal("registering path not ending with '*filepath' did not panic")
}
body := []byte("fake ico")
ioutil.WriteFile(os.TempDir()+"/favicon.ico", body, 0644)
router.ServeFiles("/*filepath", os.TempDir())
s := &fasthttp.Server{
Handler: router.Handler,
}
rw := &readWriter{}
ch := make(chan error)
rw.r.WriteString(string("GET /favicon.ico HTTP/1.1\r\n\r\n"))
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(500 * time.Millisecond):
t.Fatalf("timeout")
}
br := bufio.NewReader(&rw.w)
var resp fasthttp.Response
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if resp.Header.StatusCode() != 200 {
t.Fatalf("Unexpected status code %d. Expected %d", resp.Header.StatusCode(), 423)
}
if !bytes.Equal(resp.Body(), body) {
t.Fatalf("Unexpected body %q. Expected %q", resp.Body(), string(body))
}
}
示例2: TestRouterNotFound
func TestRouterNotFound(t *testing.T) {
handlerFunc := func(_ *fasthttp.RequestCtx, _ Params) {}
router := New()
router.GET("/path", handlerFunc)
router.GET("/dir/", handlerFunc)
router.GET("/", handlerFunc)
testRoutes := []struct {
route string
code int
}{
{"/path/", 301}, // TSR -/
{"/dir", 301}, // TSR +/
{"/", 200}, // TSR +/
{"/PATH", 301}, // Fixed Case
{"/DIR", 301}, // Fixed Case
{"/PATH/", 301}, // Fixed Case -/
{"/DIR/", 301}, // Fixed Case +/
{"/../path", 200}, // CleanPath
{"/nope", 404}, // NotFound
}
s := &fasthttp.Server{
Handler: router.Handler,
}
rw := &readWriter{}
br := bufio.NewReader(&rw.w)
var resp fasthttp.Response
ch := make(chan error)
for _, tr := range testRoutes {
rw.r.WriteString(fmt.Sprintf("GET %s HTTP/1.1\r\n\r\n", tr.route))
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
}
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if !(resp.Header.StatusCode() == tr.code) {
t.Errorf("NotFound handling route %s failed: Code=%d want=%d",
tr.route, resp.Header.StatusCode(), tr.code)
}
}
// Test custom not found handler
var notFound bool
router.NotFound = fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(404)
notFound = true
})
rw.r.WriteString("GET /nope HTTP/1.1\r\n\r\n")
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
}
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if !(resp.Header.StatusCode() == 404 && notFound == true) {
t.Errorf("Custom NotFound handler failed: Code=%d, Header=%v", resp.Header.StatusCode(), string(resp.Header.Peek("Location")))
}
// Test other method than GET (want 307 instead of 301)
router.PATCH("/path", handlerFunc)
rw.r.WriteString("PATCH /path/ HTTP/1.1\r\n\r\n")
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
}
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if !(resp.Header.StatusCode() == 307) {
t.Errorf("Custom NotFound handler failed: Code=%d, Header=%v", resp.Header.StatusCode(), string(resp.Header.Peek("Location")))
}
// Test special case where no node for the prefix "/" exists
//.........这里部分代码省略.........
示例3: TestRouterOPTIONS
func TestRouterOPTIONS(t *testing.T) {
// TODO: because fasthttp is not support OPTIONS method now,
// these test cases will be used in the future.
handlerFunc := func(_ *fasthttp.RequestCtx, _ Params) {}
router := New()
router.POST("/path", handlerFunc)
// test not allowed
// * (server)
s := &fasthttp.Server{
Handler: router.Handler,
}
rw := &readWriter{}
ch := make(chan error)
rw.r.WriteString("OPTIONS * HTTP/1.1\r\nHost:\r\n\r\n")
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
}
br := bufio.NewReader(&rw.w)
var resp fasthttp.Response
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if resp.Header.StatusCode() != fasthttp.StatusOK {
t.Errorf("OPTIONS handling failed: Code=%d, Header=%v",
resp.Header.StatusCode(), resp.Header.String())
} else if allow := string(resp.Header.Peek("Allow")); allow != "POST, OPTIONS" {
t.Error("unexpected Allow header value: " + allow)
}
// path
rw.r.WriteString("OPTIONS /path HTTP/1.1\r\n\r\n")
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
}
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if resp.Header.StatusCode() != fasthttp.StatusOK {
t.Errorf("OPTIONS handling failed: Code=%d, Header=%v",
resp.Header.StatusCode(), resp.Header.String())
} else if allow := string(resp.Header.Peek("Allow")); allow != "POST, OPTIONS" {
t.Error("unexpected Allow header value: " + allow)
}
rw.r.WriteString("OPTIONS /doesnotexist HTTP/1.1\r\n\r\n")
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
}
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if !(resp.Header.StatusCode() == fasthttp.StatusNotFound) {
t.Errorf("OPTIONS handling failed: Code=%d, Header=%v",
resp.Header.StatusCode(), resp.Header.String())
}
// add another method
router.GET("/path", handlerFunc)
// test again
// * (server)
rw.r.WriteString("OPTIONS * HTTP/1.1\r\n\r\n")
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
//.........这里部分代码省略.........
示例4: TestRouterNotAllowed
func TestRouterNotAllowed(t *testing.T) {
handlerFunc := func(_ *fasthttp.RequestCtx, _ Params) {}
router := New()
router.POST("/path", handlerFunc)
// Test not allowed
s := &fasthttp.Server{
Handler: router.Handler,
}
rw := &readWriter{}
ch := make(chan error)
rw.r.WriteString("GET /path HTTP/1.1\r\n\r\n")
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
}
br := bufio.NewReader(&rw.w)
var resp fasthttp.Response
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if !(resp.Header.StatusCode() == fasthttp.StatusMethodNotAllowed) {
t.Errorf("NotAllowed handling failed: Code=%d, Header=%v", resp.Header.StatusCode(), resp.Header)
} else if allow := string(resp.Header.Peek("Allow")); allow != "POST, OPTIONS" {
t.Error("unexpected Allow header value: " + allow)
}
// add another method
router.DELETE("/path", handlerFunc)
router.OPTIONS("/path", handlerFunc) // must be ignored
// test again
rw.r.WriteString("GET /path HTTP/1.1\r\n\r\n")
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
}
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if !(resp.Header.StatusCode() == fasthttp.StatusMethodNotAllowed) {
t.Errorf("NotAllowed handling failed: Code=%d, Header=%v", resp.Header.StatusCode(), resp.Header)
} else if allow := string(resp.Header.Peek("Allow")); allow != "POST, DELETE, OPTIONS" && allow != "DELETE, POST, OPTIONS" {
t.Error("unexpected Allow header value: " + allow)
}
responseText := "custom method"
router.MethodNotAllowed = fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusTeapot)
ctx.Write([]byte(responseText))
})
rw.r.WriteString("GET /path HTTP/1.1\r\n\r\n")
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
}
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if !bytes.Equal(resp.Body(), []byte(responseText)) {
t.Errorf("unexpected response got %q want %q", string(resp.Body()), responseText)
}
if resp.Header.StatusCode() != fasthttp.StatusTeapot {
t.Errorf("unexpected response code %d want %d", resp.Header.StatusCode(), fasthttp.StatusTeapot)
}
if allow := string(resp.Header.Peek("Allow")); allow != "POST, DELETE, OPTIONS" && allow != "DELETE, POST, OPTIONS" {
t.Error("unexpected Allow header value: " + allow)
}
}
示例5: TestRouterChaining
func TestRouterChaining(t *testing.T) {
router1 := New()
router2 := New()
router1.NotFound = router2.Handler
fooHit := false
router1.POST("/foo", func(ctx *fasthttp.RequestCtx, _ Params) {
fooHit = true
ctx.SetStatusCode(fasthttp.StatusOK)
})
barHit := false
router2.POST("/bar", func(ctx *fasthttp.RequestCtx, _ Params) {
barHit = true
ctx.SetStatusCode(fasthttp.StatusOK)
})
s := &fasthttp.Server{
Handler: router1.Handler,
}
rw := &readWriter{}
ch := make(chan error)
rw.r.WriteString("POST /foo HTTP/1.1\r\n\r\n")
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
}
br := bufio.NewReader(&rw.w)
var resp fasthttp.Response
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if !(resp.Header.StatusCode() == fasthttp.StatusOK && fooHit) {
t.Errorf("Regular routing failed with router chaining.")
t.FailNow()
}
rw.r.WriteString("POST /bar HTTP/1.1\r\n\r\n")
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
}
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if !(resp.Header.StatusCode() == fasthttp.StatusOK && barHit) {
t.Errorf("Chained routing failed with router chaining.")
t.FailNow()
}
rw.r.WriteString("POST /qax HTTP/1.1\r\n\r\n")
go func() {
ch <- s.ServeConn(rw)
}()
select {
case err := <-ch:
if err != nil {
t.Fatalf("return error %s", err)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
}
if err := resp.Read(br); err != nil {
t.Fatalf("Unexpected error when reading response: %s", err)
}
if !(resp.Header.StatusCode() == fasthttp.StatusNotFound) {
t.Errorf("NotFound behavior failed with router chaining.")
t.FailNow()
}
}