2017-06-10 02:31:50 +02:00
|
|
|
// black-box testing
|
|
|
|
package handlerconv_test
|
|
|
|
|
|
|
|
import (
|
2019-02-02 03:49:58 +01:00
|
|
|
stdContext "context"
|
2017-06-10 02:31:50 +02:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
"github.com/kataras/iris/v12/context"
|
|
|
|
"github.com/kataras/iris/v12/core/handlerconv"
|
|
|
|
"github.com/kataras/iris/v12/httptest"
|
2017-06-10 02:31:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFromStd(t *testing.T) {
|
|
|
|
expected := "ok"
|
|
|
|
std := func(w http.ResponseWriter, r *http.Request) {
|
2020-02-02 15:29:06 +01:00
|
|
|
_, err := w.Write([]byte(expected))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-06-10 02:31:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
h := handlerconv.FromStd(http.HandlerFunc(std))
|
|
|
|
|
|
|
|
hFunc := handlerconv.FromStd(std)
|
|
|
|
|
|
|
|
app := iris.New()
|
|
|
|
app.Get("/handler", h)
|
|
|
|
app.Get("/func", hFunc)
|
|
|
|
|
2017-06-15 19:02:08 +02:00
|
|
|
e := httptest.New(t, app)
|
2017-06-10 02:31:50 +02:00
|
|
|
|
|
|
|
e.GET("/handler").
|
|
|
|
Expect().Status(iris.StatusOK).Body().Equal(expected)
|
|
|
|
|
|
|
|
e.GET("/func").
|
|
|
|
Expect().Status(iris.StatusOK).Body().Equal(expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFromStdWithNext(t *testing.T) {
|
|
|
|
basicauth := "secret"
|
|
|
|
passed := "ok"
|
|
|
|
|
|
|
|
stdWNext := func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
|
|
|
if username, password, ok := r.BasicAuth(); ok &&
|
|
|
|
username == basicauth && password == basicauth {
|
2019-02-02 03:49:58 +01:00
|
|
|
ctx := stdContext.WithValue(r.Context(), "key", "ok")
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
2017-06-10 02:31:50 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(iris.StatusForbidden)
|
|
|
|
}
|
|
|
|
|
|
|
|
h := handlerconv.FromStdWithNext(stdWNext)
|
|
|
|
next := func(ctx context.Context) {
|
2019-02-02 03:49:58 +01:00
|
|
|
ctx.WriteString(ctx.Request().Context().Value("key").(string))
|
2017-06-10 02:31:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
app := iris.New()
|
|
|
|
app.Get("/handlerwithnext", h, next)
|
|
|
|
|
2017-06-15 19:02:08 +02:00
|
|
|
e := httptest.New(t, app)
|
2017-06-10 02:31:50 +02:00
|
|
|
|
|
|
|
e.GET("/handlerwithnext").
|
|
|
|
Expect().Status(iris.StatusForbidden)
|
|
|
|
|
|
|
|
e.GET("/handlerwithnext").WithBasicAuth(basicauth, basicauth).
|
|
|
|
Expect().Status(iris.StatusOK).Body().Equal(passed)
|
|
|
|
}
|