mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
d697426cb6
Change the new ctx.Compres to ctx.CompressWriter and iris.Compress and iris.CompressReader as one iris.Compression Update the README example (master development branch) Former-commit-id: fb67858fe5be5662b5816df41020c28ff9a8c6f6
47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/kataras/iris/v12"
|
|
"github.com/kataras/iris/v12/httptest"
|
|
)
|
|
|
|
// $ go test -v
|
|
func TestNewApp(t *testing.T) {
|
|
app := newApp()
|
|
e := httptest.New(t, app)
|
|
|
|
// redirects to /admin without basic auth
|
|
e.GET("/").Expect().Status(httptest.StatusUnauthorized)
|
|
// without basic auth
|
|
e.GET("/admin").Expect().Status(httptest.StatusUnauthorized)
|
|
|
|
// with valid basic auth
|
|
e.GET("/admin").WithBasicAuth("myusername", "mypassword").Expect().
|
|
Status(httptest.StatusOK).Body().Equal("/admin myusername:mypassword")
|
|
e.GET("/admin/profile").WithBasicAuth("myusername", "mypassword").Expect().
|
|
Status(httptest.StatusOK).Body().Equal("/admin/profile myusername:mypassword")
|
|
e.GET("/admin/settings").WithBasicAuth("myusername", "mypassword").Expect().
|
|
Status(httptest.StatusOK).Body().Equal("/admin/settings myusername:mypassword")
|
|
|
|
// with invalid basic auth
|
|
e.GET("/admin/settings").WithBasicAuth("invalidusername", "invalidpassword").
|
|
Expect().Status(httptest.StatusUnauthorized)
|
|
}
|
|
|
|
func TestHandlerUsingNetHTTP(t *testing.T) {
|
|
handler := func(ctx iris.Context) {
|
|
ctx.WriteString("Hello, World!")
|
|
}
|
|
|
|
// A shortcut for net/http/httptest.NewRecorder/NewRequest.
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
httptest.Do(w, r, handler)
|
|
if expected, got := "Hello, World!", w.Body.String(); expected != got {
|
|
t.Fatalf("expected body: %s but got: %s", expected, got)
|
|
}
|
|
}
|