Skip to content

Commit

Permalink
add QueryParam_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
fumeboy committed Dec 31, 2020
1 parent 0c17481 commit c13a015
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
3 changes: 2 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func (ctx *Context) parseForm() error {
if ctx.parsed {
return nil
}
if strings.HasPrefix(ctx.Request.Header.Get("Content-Type"), "multipart/form-data") {
ct := ctx.Request.Header.Get("Content-Type")
if strings.HasPrefix(ct, "multipart/form-data") {
if err := ctx.Request.ParseMultipartForm(ctx.engine.Config.MaxMultipartMemory); err != nil {
return err
}
Expand Down
14 changes: 10 additions & 4 deletions plugins/PostParam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http/httptest"
"nji"
"nji/plugins"
"strings"
"testing"
)

Expand All @@ -15,20 +16,25 @@ type b struct {
B plugins.PostParamOptional
}

func (view *b) Handle(c *nji.Context) {
func (v *b) Handle(c *nji.Context) {
c.ResponseWriter.WriteHeader(200)
_, _ = c.ResponseWriter.Write([]byte("Hello !"))
_, _ = c.ResponseWriter.Write([]byte(v.A.Value+v.B.Value))
}

func TestContextB(t *testing.T) {
app := nji.Config{
UnescapePathValues: true,
}.New()
app.POST("/api/", nji.Inject(&b{}))
r, err := http.NewRequest("POST", "/api/", nil)
reader := strings.NewReader(`A=Hello &B=World!`)
r, err := http.NewRequest(http.MethodPost, "/api/", reader)
if err != nil {
t.Error(err.Error())
return
}
app.ServeHTTP(httptest.NewRecorder(), r)
r.Header.Add("Content-Type","application/x-www-form-urlencoded")
w := httptest.NewRecorder()
app.ServeHTTP(w, r)

t.Log(string(w.Body.Bytes()))
}
36 changes: 36 additions & 0 deletions plugins/QueryParam_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package plugins_test

import (
"net/http"
"net/http/httptest"
"nji"
"nji/plugins"
"testing"
)

var _ nji.ViewI = &a{}

type c struct {
A plugins.QueryParam
B plugins.QueryParamOptional
}

func (v *c) Handle(c *nji.Context) {
c.ResponseWriter.WriteHeader(200)
_, _ = c.ResponseWriter.Write([]byte(v.A.Value+v.B.Value))
}

func TestContextC(t *testing.T) {
app := nji.Config{
UnescapePathValues: true,
}.New()
app.GET("/api/", nji.Inject(&c{}))
r, err := http.NewRequest("GET", "/api/?A=Hello &B=World!", nil)
if err != nil {
t.Error(err.Error())
return
}
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
t.Log(string(w.Body.Bytes()))
}

0 comments on commit c13a015

Please sign in to comment.