1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
19 "github.com/revel/revel"
20 "github.com/revel/revel/session"
23 func TestMisc(t *testing.T) {
24 testSuite := createNewTestSuite(t)
27 if !strings.EqualFold("127.0.0.1:9001", testSuite.Host()) {
28 t.Error("Incorrect Host value found.")
32 if !strings.EqualFold("http://127.0.0.1:9001", testSuite.BaseUrl()) {
33 t.Error("Incorrect BaseUrl http value found.")
36 if !strings.EqualFold("https://127.0.0.1:9001", testSuite.BaseUrl()) {
37 t.Error("Incorrect BaseUrl https value found.")
42 if !strings.EqualFold("ws://127.0.0.1:9001", testSuite.WebSocketUrl()) {
43 t.Error("Incorrect WebSocketUrl value found.")
46 testSuite.AssertNotEqual("Yes", "No")
47 testSuite.Assert(true)
50 func TestGet(t *testing.T) {
51 ts := createTestServer(testHandle)
54 testSuite := createNewTestSuite(t)
58 testSuite.AssertContains("this is testcase homepage")
59 testSuite.AssertNotContains("not exists")
62 func TestGetNotFound(t *testing.T) {
63 ts := createTestServer(testHandle)
66 testSuite := createNewTestSuite(t)
68 testSuite.Get("/notfound")
69 testSuite.AssertNotFound()
70 // testSuite.AssertContains("this is testcase homepage")
71 // testSuite.AssertNotContains("not exists")
74 // This test is known to fail
75 func TestGetCustom(t *testing.T) {
76 testSuite := createNewTestSuite(t)
77 for x := 0; x < 5; x++ {
78 testSuite.GetCustom("http://httpbin.org/get").Send()
79 if testSuite.Response.StatusCode == http.StatusOK {
82 println("Failed request from http://httpbin.org/get", testSuite.Response.StatusCode)
86 testSuite.AssertContentType("application/json")
87 testSuite.AssertContains("httpbin.org")
88 testSuite.AssertContainsRegex("gzip|deflate")
91 func TestDelete(t *testing.T) {
92 ts := createTestServer(testHandle)
95 testSuite := createNewTestSuite(t)
97 testSuite.Delete("/purchases/10001")
101 func TestPut(t *testing.T) {
102 ts := createTestServer(testHandle)
105 testSuite := createNewTestSuite(t)
107 testSuite.Put("/purchases/10002",
109 bytes.NewReader([]byte(`{"sku":"163645GHT", "desc":"This is test product"}`)),
111 testSuite.AssertStatus(http.StatusNoContent)
114 func TestPutForm(t *testing.T) {
115 ts := createTestServer(testHandle)
118 testSuite := createNewTestSuite(t)
121 data.Add("name", "beacon1name")
122 data.Add("value", "beacon1value")
124 testSuite.PutForm("/send", data)
125 testSuite.AssertStatus(http.StatusNoContent)
128 func TestPatch(t *testing.T) {
129 ts := createTestServer(testHandle)
132 testSuite := createNewTestSuite(t)
134 testSuite.Patch("/purchases/10003",
136 bytes.NewReader([]byte(`{"desc": "This is test patch for product"}`)),
138 testSuite.AssertStatus(http.StatusNoContent)
141 func TestPost(t *testing.T) {
142 ts := createTestServer(testHandle)
145 testSuite := createNewTestSuite(t)
147 testSuite.Post("/login",
149 bytes.NewReader([]byte(`{"username":"testuser", "password":"testpass"}`)),
152 testSuite.AssertContains("login successful")
155 func TestPostForm(t *testing.T) {
156 ts := createTestServer(testHandle)
159 testSuite := createNewTestSuite(t)
162 data.Add("username", "testuser")
163 data.Add("password", "testpassword")
165 testSuite.PostForm("/login", data)
167 testSuite.AssertContains("login successful")
170 func TestPostFileUpload(t *testing.T) {
171 ts := createTestServer(testHandle)
174 testSuite := createNewTestSuite(t)
176 params := url.Values{}
177 params.Add("first_name", "Jeevanandam")
178 params.Add("last_name", "M.")
180 currentDir, _ := os.Getwd()
181 basePath := filepath.Dir(currentDir)
183 filePaths := url.Values{}
184 filePaths.Add("revel_file", filepath.Join(basePath, "revel.go"))
185 filePaths.Add("server_file", filepath.Join(basePath, "server.go"))
186 filePaths.Add("readme_file", filepath.Join(basePath, "README.md"))
188 testSuite.PostFile("/upload", params, filePaths)
191 testSuite.AssertContains("File: revel.go")
192 testSuite.AssertContains("File: server.go")
193 testSuite.AssertNotContains("File: not_exists.go")
194 testSuite.AssertEqual("text/plain; charset=utf-8", testSuite.Response.Header.Get("Content-Type"))
198 func createNewTestSuite(t *testing.T) *TestSuite {
199 suite := NewTestSuite()
201 if suite.Client == nil || suite.SessionEngine == nil {
202 t.Error("Unable to create a testsuite")
208 func testHandle(w http.ResponseWriter, r *http.Request) {
209 if r.Method == "GET" {
210 if r.URL.Path == "/" {
211 _, _ = w.Write([]byte(`this is testcase homepage`))
216 if r.Method == "POST" {
217 if r.URL.Path == "/login" {
218 http.SetCookie(w, &http.Cookie{
219 Name: session.SessionCookieSuffix,
220 Value: "This is simple session value",
224 Expires: time.Now().Add(time.Minute * 5).UTC(),
227 w.Header().Set("Content-Type", "application/json")
228 _, _ = w.Write([]byte(`{ "id": "success", "message": "login successful" }`))
232 handleFileUpload(w, r)
236 if r.Method == "DELETE" {
237 if r.URL.Path == "/purchases/10001" {
238 w.WriteHeader(http.StatusOK)
243 if r.Method == "PUT" {
244 if r.URL.Path == "/purchases/10002" {
245 w.WriteHeader(http.StatusNoContent)
249 if r.URL.Path == "/send" {
250 w.WriteHeader(http.StatusNoContent)
255 if r.Method == "PATCH" {
256 if r.URL.Path == "/purchases/10003" {
257 w.WriteHeader(http.StatusNoContent)
262 w.WriteHeader(http.StatusNotFound)
265 func handleFileUpload(w http.ResponseWriter, r *http.Request) {
266 if r.URL.Path == "/upload" {
267 _ = r.ParseMultipartForm(10e6)
269 for _, fhdrs := range r.MultipartForm.File {
270 for _, hdr := range fhdrs {
271 dotPos := strings.LastIndex(hdr.Filename, ".")
272 fname := fmt.Sprintf("%s-%v%s", hdr.Filename[:dotPos], time.Now().Unix(), hdr.Filename[dotPos:])
273 _, _ = buf.WriteString(fmt.Sprintf(
274 "Firstname: %v\nLastname: %v\nFile: %v\nHeader: %v\nUploaded as: %v\n",
275 r.FormValue("first_name"),
276 r.FormValue("last_name"),
283 _, _ = w.Write(buf.Bytes())
289 func createTestServer(fn func(w http.ResponseWriter, r *http.Request)) *httptest.Server {
290 testServer := httptest.NewServer(http.HandlerFunc(fn))
291 revel.ServerEngineInit.Address = testServer.URL[7:]
296 if revel.ServerEngineInit == nil {
297 revel.ServerEngineInit = &revel.EngineInit{