Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TestSetParamContext to sets ParamHolder for testing #8

Merged
merged 1 commit into from
May 27, 2016

Conversation

achiku
Copy link
Contributor

@achiku achiku commented May 27, 2016

As discussed in #7, this function is purely for testing, and set ParamHolder in context.Context.

func helloWithParam(ctx context.Context, w http.ResponseWriter, r *http.Request) {
    n := xmux.Param(ctx, "name")
    fmt.Fprintf(w, "Hello, %s!", n)
    return
}

func main() {
    m := xmux.New()
    m.GET("/hello/:name", xhandler.HandlerFuncC(helloWithParam))
    if err := http.ListenAndServe(":8888", xhandler.New(context.Background(), m)); err != nil {
        log.Fatal(err)
    }
}
func TestHelloWithParam(t *testing.T) {
    n := "dummyName"
    ps := xmux.ParamHolder{xmux.Parameter{Name: "name", Value: n}}
    ctx := xmux.TestSetParamContext(context.TODO(), ps)
    req, _ := http.NewRequest("GET", "/hello/"+n, nil)
    w := httptest.NewRecorder()
    helloWithParam(ctx, w, req)
    t.Log(w.Body)
}

@rs rs merged commit 447ce20 into rs:master May 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants