Gisle Aune
7 years ago
4 changed files with 73 additions and 9 deletions
-
18function.go
-
42function_test.go
-
16resource.go
-
6router.go
@ -0,0 +1,18 @@ |
|||
package wrouter |
|||
|
|||
import ( |
|||
"net/http" |
|||
|
|||
"git.aiterp.net/gisle/wrouter/auth" |
|||
) |
|||
|
|||
// FunctionHandlerFunc is simply a function that is called directly rather than through a struct
|
|||
type FunctionHandlerFunc func(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool |
|||
|
|||
type functionHandler struct { |
|||
function FunctionHandlerFunc |
|||
} |
|||
|
|||
func (handler *functionHandler) Handle(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool { |
|||
return handler.function(path, w, req, user) |
|||
} |
@ -0,0 +1,42 @@ |
|||
package wrouter |
|||
|
|||
import ( |
|||
"io/ioutil" |
|||
"net/http" |
|||
"net/http/httptest" |
|||
"testing" |
|||
|
|||
"git.aiterp.net/gisle/wrouter/response" |
|||
|
|||
"git.aiterp.net/gisle/wrouter/auth" |
|||
) |
|||
|
|||
func TestFunction(t *testing.T) { |
|||
router := &Router{} |
|||
router.Function("/test", func(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool { |
|||
response.Text(w, 200, path+" called") |
|||
return true |
|||
}) |
|||
server := httptest.NewServer(router) |
|||
|
|||
resp, err := http.Get(server.URL + "/test/24g42g24f24f24f24f") |
|||
if err != nil { |
|||
t.Error("Request:", err) |
|||
t.Fail() |
|||
} |
|||
|
|||
if resp.StatusCode != 200 { |
|||
t.Error("Expected 200, got", resp.Status) |
|||
t.Fail() |
|||
} |
|||
|
|||
if resp.ContentLength == 0 { |
|||
t.Error("No content returned from server") |
|||
t.Fail() |
|||
} |
|||
|
|||
data, _ := ioutil.ReadAll(resp.Body) |
|||
if string(data) != "/test called" { |
|||
t.Errorf(`Body: "%s" != "%s"`, string(data), "/test called") |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue