// TestFileServeHandler tests the serving of files. func TestFileServeHandler(t *testing.T) { assert := audit.NewTestingAssertion(t, true) // Setup the test file. dir, err := ioutil.TempDir("", "gont-web") assert.Nil(err) defer os.RemoveAll(dir) filename := filepath.Join(dir, "foo.txt") f, err := os.Create(filename) assert.Nil(err) _, err = f.WriteString("Been there, done that!") assert.Nil(err) assert.Logf("written %s", f.Name()) err = f.Close() assert.Nil(err) // Setup the test server. mux, ts, err := web.StartTestServer() assert.Nil(err) defer ts.Close() err = mux.Register("test", "files", web.NewFileServeHandler("files", dir)) assert.Nil(err) // Perform test requests. resp, err := web.DoTestRequest(ts, &web.TestRequest{ Method: "GET", Path: "/test/files/foo.txt", }) assert.Nil(err) assert.Equal(string(resp.Body), "Been there, done that!") resp, err = web.DoTestRequest(ts, &web.TestRequest{ Method: "GET", Path: "/test/files/does.not.exist", }) assert.Nil(err) assert.Equal(string(resp.Body), "404 page not found\n") }
// TestPutGOB tests the PUT command with a GOB payload and result. func TestPutGOB(t *testing.T) { assert := audit.NewTestingAssertion(t, true) // Setup the test server. mux, ts, err := web.StartTestServer() assert.Nil(err) defer ts.Close() err = mux.Register("test", "gob", NewTestHandler("putgob", assert)) assert.Nil(err) // Perform test requests. reqData := TestCounterData{"test", 4711} reqBuf := new(bytes.Buffer) err = gob.NewEncoder(reqBuf).Encode(reqData) assert.Nil(err, "GOB encode.") t.Logf("%q", reqBuf.String()) resp, err := web.DoTestRequest(ts, &web.TestRequest{ Method: "POST", Path: "/test/gob", Header: web.TestSettings{"Content-Type": "application/vnd.tideland.gob"}, Body: reqBuf.Bytes(), }) var respData TestCounterData err = gob.NewDecoder(bytes.NewBuffer(resp.Body)).Decode(&respData) assert.Nil(err, "GOB decode.") assert.Equal(respData.ID, "test", "GOB decoded 'id'.") assert.Equal(respData.Count, int64(4711), "GOB decoded 'count'.") }
// TestMethodNotSupported tests the handling of a not support HTTP method. func TestMethodNotSupported(t *testing.T) { assert := audit.NewTestingAssertion(t, true) // Setup the test server. mux, ts, err := web.StartTestServer() assert.Nil(err) defer ts.Close() err = mux.Register("test", "method", NewTestHandler("method", assert)) assert.Nil(err) // Perform test requests. resp, err := web.DoTestRequest(ts, &web.TestRequest{ Method: "OPTION", Path: "/test/method", }) assert.Nil(err) assert.Substring("OPTION", string(resp.Body)) }
// TestFallbackDefault tests the fallback to default. func TestFallbackDefault(t *testing.T) { assert := audit.NewTestingAssertion(t, true) // Setup the test server. mux, ts, err := web.StartTestServer() assert.Nil(err) defer ts.Close() err = mux.Register("default", "default", NewTestHandler("default", assert)) assert.Nil(err) // Perform test requests. resp, err := web.DoTestRequest(ts, &web.TestRequest{ Method: "GET", Path: "/x/y", }) assert.Nil(err) assert.Substring("<li>Resource: y</li>", string(resp.Body)) }
// TestLongPath tests the setting of long path tail as resource ID. func TestLongPath(t *testing.T) { assert := audit.NewTestingAssertion(t, true) // Setup the test server. mux, ts, err := web.StartTestServer() assert.Nil(err) defer ts.Close() err = mux.Register("content", "blog", NewTestHandler("default", assert)) assert.Nil(err) // Perform test requests. resp, err := web.DoTestRequest(ts, &web.TestRequest{ Method: "GET", Path: "/content/blog/2014/09/30/just-a-test", }) assert.Nil(err) assert.Substring("<li>Resource ID: 2014/09/30/just-a-test</li>", string(resp.Body)) }
// TestGetXML tests the GET command with an XML result. func TestGetXML(t *testing.T) { assert := audit.NewTestingAssertion(t, true) // Setup the test server. mux, ts, err := web.StartTestServer() assert.Nil(err) defer ts.Close() err = mux.Register("test", "xml", NewTestHandler("xml", assert)) assert.Nil(err) // Perform test requests. resp, err := web.DoTestRequest(ts, &web.TestRequest{ Method: "GET", Path: "/test/xml/4711", Header: web.TestSettings{"Accept": "application/xml"}, }) assert.Nil(err) assert.Substring("<ResourceID>4711</ResourceID>", string(resp.Body)) }
// TestWrapperHandler tests the usage of standard handler funcs // wrapped to be used inside the package context. func TestWrapperHandler(t *testing.T) { assert := audit.NewTestingAssertion(t, true) // Setup the test server. mux, ts, err := web.StartTestServer() assert.Nil(err) defer ts.Close() handler := func(rw http.ResponseWriter, r *http.Request) { rw.Write([]byte("Been there, done that!")) } err = mux.Register("test", "wrapper", web.NewWrapperHandler("wrapper", handler)) assert.Nil(err) // Perform test requests. resp, err := web.DoTestRequest(ts, &web.TestRequest{ Method: "GET", Path: "/test/wrapper", }) assert.Nil(err) assert.Equal(string(resp.Body), "Been there, done that!") }
// TestGetJSON tests the GET command with a JSON result. func TestGetJSON(t *testing.T) { assert := audit.NewTestingAssertion(t, true) // Setup the test server. mux, ts, err := web.StartTestServer() assert.Nil(err) defer ts.Close() err = mux.Register("test", "json", NewTestHandler("json", assert)) assert.Nil(err) // Perform test requests. resp, err := web.DoTestRequest(ts, &web.TestRequest{ Method: "GET", Path: "/test/json/4711", Header: web.TestSettings{"Accept": "application/json"}, }) assert.Nil(err, "Local JSON GET.") var data TestRequestData err = json.Unmarshal(resp.Body, &data) assert.Nil(err) assert.Equal(data.ResourceID, "4711") }
// TestFileUploadHandler tests the uploading of files. func TestFileUploadHandler(t *testing.T) { assert := audit.NewTestingAssertion(t, true) data := "Been there, done that!" // Setup the file upload processor. processor := func(ctx web.Context, header *multipart.FileHeader, file multipart.File) error { assert.Equal(header.Filename, "test.txt") scanner := bufio.NewScanner(file) assert.True(scanner.Scan()) text := scanner.Text() assert.Equal(text, data) return nil } // Setup the test server. mux, ts, err := web.StartTestServer() assert.Nil(err) defer ts.Close() err = mux.Register("test", "files", web.NewFileUploadHandler("files", processor)) assert.Nil(err) // Perform test requests. _, err = web.DoTestUpload(ts, "/test/files", "testfile", "test.txt", data) assert.Nil(err) }
// TestPutJSON tests the PUT command with a JSON payload and result. func TestPutJSON(t *testing.T) { assert := audit.NewTestingAssertion(t, true) // Setup the test server. mux, ts, err := web.StartTestServer() assert.Nil(err) defer ts.Close() err = mux.Register("test", "json", NewTestHandler("json", assert)) assert.Nil(err) // Perform test requests. reqData := TestRequestData{"foo", "bar", "4711"} reqBuf, _ := json.Marshal(reqData) resp, err := web.DoTestRequest(ts, &web.TestRequest{ Method: "PUT", Path: "/test/json/4711", Header: web.TestSettings{"Content-Type": "application/json", "Accept": "application/json"}, Body: reqBuf, }) assert.Nil(err) var recvData TestRequestData err = json.Unmarshal(resp.Body, &recvData) assert.Nil(err) assert.Equal(recvData, reqData) }
// TestHandlerStack tests a complete handler stack. func TestHandlerStack(t *testing.T) { assert := audit.NewTestingAssertion(t, true) // Setup the test server. sm := web.NewCookieSceneManager(5 * time.Minute) mux, ts, err := web.StartTestServer(web.Scenes(sm)) assert.Nil(err) defer ts.Close() err = mux.RegisterAll(web.Registrations{ {"authentication", "login", NewTestHandler("login", assert)}, {"test", "stack", NewAuthHandler("foo", assert)}, {"test", "stack", NewTestHandler("stack", assert)}, }) assert.Nil(err) // Perform test requests. resp, err := web.DoTestRequest(ts, &web.TestRequest{ Method: "GET", Path: "/test/stack", }) sceneID := resp.Cookies["sceneID"] assert.Substring("<li>Resource: login</li>", string(resp.Body)) resp, err = web.DoTestRequest(ts, &web.TestRequest{ Method: "GET", Path: "/test/stack", Cookies: web.TestSettings{"sceneID": sceneID}, Header: web.TestSettings{"password": "******"}, }) assert.Nil(err) assert.Substring("<li>Resource: stack</li>", string(resp.Body)) resp, err = web.DoTestRequest(ts, &web.TestRequest{ Method: "GET", Path: "/test/stack", Cookies: web.TestSettings{"sceneID": sceneID}, Header: web.TestSettings{"password": "******"}, }) assert.Nil(err) assert.Substring("<li>Resource: stack</li>", string(resp.Body)) }