How to get all route handler for testing #439

Closed
appleboy opened this Issue Mar 31, 2016 · 5 comments

Projects

None yet

2 participants

@appleboy

Hi @labstack @Gouthamve

According to #293.

I want to get echo all router handler for testing.

package main

import (
    "fmt"
    "net/http"
    "net/http/httptest"

    "github.com/labstack/echo"
)

func main() {
    e := echo.New()

    e.Get("/", hello())

    router := e.Router()

    req, _ := http.NewRequest("GET", "/", nil)
    w := httptest.NewRecorder()

    router.ServeHTTP(w, req)
    fmt.Println(w.Code)
    fmt.Println(w.Body.String())
}

// Handler
func hello() echo.HandlerFunc {
    return func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello World")
    }
}

I got the following error message:

./a.go:21: router.ServeHTTP undefined (type *echo.Router has no field or method ServeHTTP)

I don't want to use the following way like this.

rq := test.NewRequest(rc.Method, rc.Path, strings.NewReader(rc.Body))
rec := test.NewResponseRecorder()
e.ServeHTTP(rq, rec)

Thanks.

@vishr
Member
vishr commented Mar 31, 2016

Things have changed in v2, it doesn't directly accept standard http.Request and http.ResponseWriter. This has been done to provide support for fasthttp server. What exactly are you trying to achieve?

@appleboy

Hi @vishr

I create the gofight project for API testing. I want to testing all golang route framework using standard http.Request and http.ResponseWriter.

func TestEchoHelloWorld(t *testing.T) {
  r := mocha.New()

  r.GET("/").
    SetDebug(true).
    RunEcho(EchoEngine(), func(r gofight.EchoHttpResponse, rq gofight.EchoHttpRequest) {
      assert.Equal(t, "Hello World", r.Body.String())
      assert.Equal(t, http.StatusOK, r.Status())
    })
}

I need integrate RunEcho func with Run func

Thanks for your reply quickly.

@vishr
Member
vishr commented Mar 31, 2016

You should use engine.Request and engine.Response for testing. What's the problem with test package?

@appleboy

@vishr

I want uniform Interface for request and response. If I want to test response status code, you must call r.status() not r.Code in echo framework.

@appleboy appleboy referenced this issue in appleboy/gofight Nov 16, 2016
Merged

Support echo v3.0.0 #40

@appleboy

Update gofight to v2.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment