|
| 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. |
| 4 | + |
| 5 | +package testing |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "net/url" |
| 13 | + "os" |
| 14 | + "path/filepath" |
| 15 | + "strings" |
| 16 | + "testing" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/revel/revel" |
| 20 | +) |
| 21 | + |
| 22 | +func TestMisc(t *testing.T) { |
| 23 | + testSuite := createNewTestSuite(t) |
| 24 | + |
| 25 | + // test Host value |
| 26 | + if !strings.EqualFold("127.0.0.1:9001", testSuite.Host()) { |
| 27 | + t.Error("Incorrect Host value found.") |
| 28 | + } |
| 29 | + |
| 30 | + // test BaseUrl |
| 31 | + if !strings.EqualFold("http://127.0.0.1:9001", testSuite.BaseUrl()) { |
| 32 | + t.Error("Incorrect BaseUrl http value found.") |
| 33 | + } |
| 34 | + revel.HttpSsl = true |
| 35 | + if !strings.EqualFold("https://127.0.0.1:9001", testSuite.BaseUrl()) { |
| 36 | + t.Error("Incorrect BaseUrl https value found.") |
| 37 | + } |
| 38 | + revel.HttpSsl = false |
| 39 | + |
| 40 | + // test WebSocketUrl |
| 41 | + if !strings.EqualFold("ws://127.0.0.1:9001", testSuite.WebSocketUrl()) { |
| 42 | + t.Error("Incorrect WebSocketUrl value found.") |
| 43 | + } |
| 44 | + |
| 45 | + testSuite.AssertNotEqual("Yes", "No") |
| 46 | + testSuite.Assert(true) |
| 47 | +} |
| 48 | + |
| 49 | +func TestGet(t *testing.T) { |
| 50 | + ts := createTestServer(testHandle) |
| 51 | + defer ts.Close() |
| 52 | + |
| 53 | + testSuite := createNewTestSuite(t) |
| 54 | + |
| 55 | + testSuite.Get("/") |
| 56 | + testSuite.AssertOk() |
| 57 | + testSuite.AssertContains("this is testcase homepage") |
| 58 | + testSuite.AssertNotContains("not exists") |
| 59 | +} |
| 60 | + |
| 61 | +func TestGetNotFound(t *testing.T) { |
| 62 | + ts := createTestServer(testHandle) |
| 63 | + defer ts.Close() |
| 64 | + |
| 65 | + testSuite := createNewTestSuite(t) |
| 66 | + |
| 67 | + testSuite.Get("/notfound") |
| 68 | + testSuite.AssertNotFound() |
| 69 | + // testSuite.AssertContains("this is testcase homepage") |
| 70 | + // testSuite.AssertNotContains("not exists") |
| 71 | +} |
| 72 | + |
| 73 | +func TestGetCustom(t *testing.T) { |
| 74 | + testSuite := createNewTestSuite(t) |
| 75 | + testSuite.GetCustom("http://httpbin.org/get").Send() |
| 76 | + |
| 77 | + testSuite.AssertOk() |
| 78 | + testSuite.AssertContentType("application/json") |
| 79 | + testSuite.AssertHeader("Server", "nginx") |
| 80 | + testSuite.AssertContains("httpbin.org") |
| 81 | + testSuite.AssertContainsRegex("gzip|deflate") |
| 82 | +} |
| 83 | + |
| 84 | +func TestDelete(t *testing.T) { |
| 85 | + ts := createTestServer(testHandle) |
| 86 | + defer ts.Close() |
| 87 | + |
| 88 | + testSuite := createNewTestSuite(t) |
| 89 | + |
| 90 | + testSuite.Delete("/purchases/10001") |
| 91 | + testSuite.AssertOk() |
| 92 | +} |
| 93 | + |
| 94 | +func TestPut(t *testing.T) { |
| 95 | + ts := createTestServer(testHandle) |
| 96 | + defer ts.Close() |
| 97 | + |
| 98 | + testSuite := createNewTestSuite(t) |
| 99 | + |
| 100 | + testSuite.Put("/purchases/10002", |
| 101 | + "application/json", |
| 102 | + bytes.NewReader([]byte(`{"sku":"163645GHT", "desc":"This is test product"}`)), |
| 103 | + ) |
| 104 | + testSuite.AssertStatus(http.StatusNoContent) |
| 105 | +} |
| 106 | + |
| 107 | +func TestPutForm(t *testing.T) { |
| 108 | + ts := createTestServer(testHandle) |
| 109 | + defer ts.Close() |
| 110 | + |
| 111 | + testSuite := createNewTestSuite(t) |
| 112 | + |
| 113 | + data := url.Values{} |
| 114 | + data.Add("name", "beacon1name") |
| 115 | + data.Add("value", "beacon1value") |
| 116 | + |
| 117 | + testSuite.PutForm("/send", data) |
| 118 | + testSuite.AssertStatus(http.StatusNoContent) |
| 119 | +} |
| 120 | + |
| 121 | +func TestPatch(t *testing.T) { |
| 122 | + ts := createTestServer(testHandle) |
| 123 | + defer ts.Close() |
| 124 | + |
| 125 | + testSuite := createNewTestSuite(t) |
| 126 | + |
| 127 | + testSuite.Patch("/purchases/10003", |
| 128 | + "application/json", |
| 129 | + bytes.NewReader([]byte(`{"desc": "This is test patch for product"}`)), |
| 130 | + ) |
| 131 | + testSuite.AssertStatus(http.StatusNoContent) |
| 132 | +} |
| 133 | + |
| 134 | +func TestPost(t *testing.T) { |
| 135 | + ts := createTestServer(testHandle) |
| 136 | + defer ts.Close() |
| 137 | + |
| 138 | + testSuite := createNewTestSuite(t) |
| 139 | + fmt.Println(testSuite.Session.Cookie().Name) |
| 140 | + |
| 141 | + testSuite.Post("/login", |
| 142 | + "application/json", |
| 143 | + bytes.NewReader([]byte(`{"username":"testuser", "password":"testpass"}`)), |
| 144 | + ) |
| 145 | + testSuite.AssertOk() |
| 146 | + testSuite.AssertContains("login successful") |
| 147 | +} |
| 148 | + |
| 149 | +func TestPostForm(t *testing.T) { |
| 150 | + ts := createTestServer(testHandle) |
| 151 | + defer ts.Close() |
| 152 | + |
| 153 | + testSuite := createNewTestSuite(t) |
| 154 | + |
| 155 | + data := url.Values{} |
| 156 | + data.Add("username", "testuser") |
| 157 | + data.Add("password", "testpassword") |
| 158 | + |
| 159 | + testSuite.PostForm("/login", data) |
| 160 | + testSuite.AssertOk() |
| 161 | + testSuite.AssertContains("login successful") |
| 162 | +} |
| 163 | + |
| 164 | +func TestPostFileUpload(t *testing.T) { |
| 165 | + ts := createTestServer(testHandle) |
| 166 | + defer ts.Close() |
| 167 | + |
| 168 | + testSuite := createNewTestSuite(t) |
| 169 | + |
| 170 | + params := url.Values{} |
| 171 | + params.Add("first_name", "Jeevanandam") |
| 172 | + params.Add("last_name", "M.") |
| 173 | + |
| 174 | + currentDir, _ := os.Getwd() |
| 175 | + basePath := filepath.Dir(currentDir) |
| 176 | + |
| 177 | + filePaths := url.Values{} |
| 178 | + filePaths.Add("revel_file", filepath.Join(basePath, "revel.go")) |
| 179 | + filePaths.Add("server_file", filepath.Join(basePath, "server.go")) |
| 180 | + filePaths.Add("readme_file", filepath.Join(basePath, "README.md")) |
| 181 | + |
| 182 | + testSuite.PostFile("/upload", params, filePaths) |
| 183 | + |
| 184 | + testSuite.AssertOk() |
| 185 | + testSuite.AssertContains("File: revel.go") |
| 186 | + testSuite.AssertContains("File: server.go") |
| 187 | + testSuite.AssertNotContains("File: not_exists.go") |
| 188 | + testSuite.AssertEqual("text/plain; charset=utf-8", testSuite.Response.Header.Get("Content-Type")) |
| 189 | + |
| 190 | +} |
| 191 | + |
| 192 | +func createNewTestSuite(t *testing.T) *TestSuite { |
| 193 | + suite := NewTestSuite() |
| 194 | + |
| 195 | + if suite.Client == nil || suite.Session == nil { |
| 196 | + t.Error("Unable to create a testsuite") |
| 197 | + } |
| 198 | + |
| 199 | + return &suite |
| 200 | +} |
| 201 | + |
| 202 | +func testHandle(w http.ResponseWriter, r *http.Request) { |
| 203 | + if r.Method == "GET" { |
| 204 | + if r.URL.Path == "/" { |
| 205 | + _, _ = w.Write([]byte(`this is testcase homepage`)) |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + if r.Method == "POST" { |
| 210 | + if r.URL.Path == "/login" { |
| 211 | + http.SetCookie(w, &http.Cookie{ |
| 212 | + Name: "_SESSION", |
| 213 | + Value: "This is simple session value", |
| 214 | + Path: "/", |
| 215 | + HttpOnly: true, |
| 216 | + Secure: false, |
| 217 | + Expires: time.Now().Add(time.Minute * 5).UTC(), |
| 218 | + }) |
| 219 | + |
| 220 | + w.Header().Set("Content-Type", "application/json") |
| 221 | + _, _ = w.Write([]byte(`{ "id": "success", "message": "login successful" }`)) |
| 222 | + return |
| 223 | + } |
| 224 | + |
| 225 | + handleFileUpload(w, r) |
| 226 | + } |
| 227 | + |
| 228 | + if r.Method == "DELETE" { |
| 229 | + if r.URL.Path == "/purchases/10001" { |
| 230 | + w.WriteHeader(http.StatusOK) |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + if r.Method == "PUT" { |
| 235 | + if r.URL.Path == "/purchases/10002" { |
| 236 | + w.WriteHeader(http.StatusNoContent) |
| 237 | + return |
| 238 | + } |
| 239 | + |
| 240 | + if r.URL.Path == "/send" { |
| 241 | + w.WriteHeader(http.StatusNoContent) |
| 242 | + return |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + if r.Method == "PATCH" { |
| 247 | + if r.URL.Path == "/purchases/10003" { |
| 248 | + w.WriteHeader(http.StatusNoContent) |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + w.WriteHeader(http.StatusNotFound) |
| 253 | + |
| 254 | +} |
| 255 | + |
| 256 | +func handleFileUpload(w http.ResponseWriter, r *http.Request) { |
| 257 | + if r.URL.Path == "/upload" { |
| 258 | + _ = r.ParseMultipartForm(10e6) |
| 259 | + |
| 260 | + for _, fhdrs := range r.MultipartForm.File { |
| 261 | + for _, hdr := range fhdrs { |
| 262 | + dotPos := strings.LastIndex(hdr.Filename, ".") |
| 263 | + fname := fmt.Sprintf("%s-%v%s", hdr.Filename[:dotPos], time.Now().Unix(), hdr.Filename[dotPos:]) |
| 264 | + _, _ = w.Write([]byte(fmt.Sprintf("Firstname: %v\nLastname: %v\nFile: %v\nHeader: %v\nUploaded as: %v\n", |
| 265 | + r.FormValue("first_name"), r.FormValue("last_name"), hdr.Filename, hdr.Header, fname))) |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + return |
| 270 | + } |
| 271 | +} |
| 272 | + |
| 273 | +func createTestServer(fn func(w http.ResponseWriter, r *http.Request)) *httptest.Server { |
| 274 | + testServer := httptest.NewServer(http.HandlerFunc(fn)) |
| 275 | + revel.Server.Addr = testServer.URL[7:] |
| 276 | + return testServer |
| 277 | +} |
| 278 | + |
| 279 | +func init() { |
| 280 | + if revel.Server == nil { |
| 281 | + revel.Server = &http.Server{ |
| 282 | + Addr: ":9001", |
| 283 | + } |
| 284 | + } |
| 285 | +} |
0 commit comments