guptachirag
guptachirag

Reputation: 131

Beego Endpoint Testing - Empty Request URI

This is what my controller looks like

func (o *MyController) Get() {
    uri := o.Ctx.Input.URI()
    beego.Trace(uri)
}

Now if i make a request to the corresponding endpoint for example "http://localhost:8081/path?key=value", i get the correct value for uri

But if i test it with following code

func TestGet(t *testing.T) {
    Convey("valid request case", t, func() {
        w := httptest.NewRecorder()
        uri := "/path?key=value"
        r, _ := http.NewRequest("GET", uri, nil)
        beego.BeeApp.Handlers.ServeHTTP(w, r)
        So(w.Code, ShouldEqual, 200)
    })
}

uri in controller is empty string

However, o.Ctx.Request.URL.Path has correct value i.e. '/path'

Upvotes: 1

Views: 642

Answers (1)

guptachirag
guptachirag

Reputation: 131

According to docs https://golang.org/pkg/net/http/#NewRequest

NewRequest returns a Request suitable for use with Client.Do or Transport.RoundTrip. To create a request for use with testing a Server Handler use either ReadRequest or manually update the Request fields.

For golang 1.7, following method should be used for creating request for testing https://golang.org/pkg/net/http/httptest/#NewRequest

NewRequest returns a new incoming server Request, suitable for passing to an http.Handler for testing.

Upvotes: 0

Related Questions