LEFT | RIGHT |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Tests for package cgi | 5 // Tests for package cgi |
6 | 6 |
7 package cgi | 7 package cgi |
8 | 8 |
9 import ( | 9 import ( |
10 "bufio" | 10 "bufio" |
11 "exec" | 11 "exec" |
12 "fmt" | 12 "fmt" |
13 "http" | 13 "http" |
14 "http/httptest" | 14 "http/httptest" |
15 "os" | 15 "os" |
16 "path/filepath" | 16 "path/filepath" |
17 "strings" | 17 "strings" |
18 "testing" | 18 "testing" |
| 19 "runtime" |
19 ) | 20 ) |
20 | 21 |
21 func newRequest(httpreq string) *http.Request { | 22 func newRequest(httpreq string) *http.Request { |
22 buf := bufio.NewReader(strings.NewReader(httpreq)) | 23 buf := bufio.NewReader(strings.NewReader(httpreq)) |
23 req, err := http.ReadRequest(buf) | 24 req, err := http.ReadRequest(buf) |
24 if err != nil { | 25 if err != nil { |
25 panic("cgi: bogus http request in test: " + httpreq) | 26 panic("cgi: bogus http request in test: " + httpreq) |
26 } | 27 } |
27 req.RemoteAddr = "1.2.3.4" | 28 req.RemoteAddr = "1.2.3.4" |
28 return req | 29 return req |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 Root: "/test.cgi", | 297 Root: "/test.cgi", |
297 PathLocationHandler: baseHandler, | 298 PathLocationHandler: baseHandler, |
298 } | 299 } |
299 expectedMap := map[string]string{ | 300 expectedMap := map[string]string{ |
300 "basepath": "/foo", | 301 "basepath": "/foo", |
301 "remoteaddr": "1.2.3.4", | 302 "remoteaddr": "1.2.3.4", |
302 } | 303 } |
303 runCgiTest(t, h, "GET /test.cgi?loc=/foo HTTP/1.0\nHost: example.com\n\n
", expectedMap) | 304 runCgiTest(t, h, "GET /test.cgi?loc=/foo HTTP/1.0\nHost: example.com\n\n
", expectedMap) |
304 } | 305 } |
305 | 306 |
306 func TestDir(t *testing.T) { | 307 func TestDirUnix(t *testing.T) { |
307 » if skipTest(t) { | 308 » if runtime.GOOS == "windows" { |
308 » » return | 309 » » return |
309 » } | 310 » } |
| 311 |
310 cwd, _ := os.Getwd() | 312 cwd, _ := os.Getwd() |
311 h := &Handler{ | 313 h := &Handler{ |
312 Path: "testdata/test.cgi", | 314 Path: "testdata/test.cgi", |
313 Root: "/test.cgi", | 315 Root: "/test.cgi", |
314 Dir: cwd, | 316 Dir: cwd, |
315 } | 317 } |
316 expectedMap := map[string]string{ | 318 expectedMap := map[string]string{ |
317 "cwd": cwd, | 319 "cwd": cwd, |
318 } | 320 } |
319 runCgiTest(t, h, "GET /test.cgi HTTP/1.0\nHost: example.com\n\n", expect
edMap) | 321 runCgiTest(t, h, "GET /test.cgi HTTP/1.0\nHost: example.com\n\n", expect
edMap) |
320 | 322 |
321 cwd, _ = os.Getwd() | 323 cwd, _ = os.Getwd() |
322 cwd = filepath.Join(cwd, "testdata") | 324 cwd = filepath.Join(cwd, "testdata") |
323 h = &Handler{ | 325 h = &Handler{ |
324 Path: "testdata/test.cgi", | 326 Path: "testdata/test.cgi", |
325 Root: "/test.cgi", | 327 Root: "/test.cgi", |
326 } | 328 } |
327 expectedMap = map[string]string{ | 329 expectedMap = map[string]string{ |
328 "cwd": cwd, | 330 "cwd": cwd, |
329 } | 331 } |
330 runCgiTest(t, h, "GET /test.cgi HTTP/1.0\nHost: example.com\n\n", expect
edMap) | 332 runCgiTest(t, h, "GET /test.cgi HTTP/1.0\nHost: example.com\n\n", expect
edMap) |
331 | 333 } |
332 } | 334 |
| 335 func TestDirWindows(t *testing.T) { |
| 336 » if runtime.GOOS != "windows" { |
| 337 » » return |
| 338 » } |
| 339 |
| 340 » cgifile, _ := filepath.Abs("testdata/test.cgi") |
| 341 |
| 342 » var perl string |
| 343 » var err os.Error |
| 344 » perl, err = exec.LookPath("perl") |
| 345 » if err != nil { |
| 346 » » return |
| 347 » } |
| 348 » perl, _ = filepath.Abs(perl) |
| 349 |
| 350 » cwd, _ := os.Getwd() |
| 351 » h := &Handler{ |
| 352 » » Path: perl, |
| 353 » » Root: "/test.cgi", |
| 354 » » Dir: cwd, |
| 355 » » Args: []string{cgifile}, |
| 356 » » Env: []string{"SCRIPT_FILENAME=" + cgifile}, |
| 357 » } |
| 358 » expectedMap := map[string]string{ |
| 359 » » "cwd": cwd, |
| 360 » } |
| 361 » runCgiTest(t, h, "GET /test.cgi HTTP/1.0\nHost: example.com\n\n", expect
edMap) |
| 362 |
| 363 » // If not specify Dir on windows, working directory should be |
| 364 » // base directory of perl. |
| 365 » cwd, _ = filepath.Split(perl) |
| 366 » if cwd != "" && cwd[len(cwd)-1] == filepath.Separator { |
| 367 » » cwd = cwd[:len(cwd)-1] |
| 368 » } |
| 369 » h = &Handler{ |
| 370 » » Path: perl, |
| 371 » » Root: "/test.cgi", |
| 372 » » Args: []string{cgifile}, |
| 373 » » Env: []string{"SCRIPT_FILENAME=" + cgifile}, |
| 374 » } |
| 375 » expectedMap = map[string]string{ |
| 376 » » "cwd": cwd, |
| 377 » } |
| 378 » runCgiTest(t, h, "GET /test.cgi HTTP/1.0\nHost: example.com\n\n", expect
edMap) |
| 379 } |
LEFT | RIGHT |