scan_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package scan
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. )
  7. var (
  8. handler, _ = NewHandler("")
  9. ts = httptest.NewServer(handler)
  10. )
  11. func TestBadRequest(t *testing.T) {
  12. // Test request with no host
  13. req, _ := http.NewRequest("GET", ts.URL, nil)
  14. resp, _ := http.DefaultClient.Do(req)
  15. if resp.StatusCode != http.StatusBadRequest {
  16. t.Fatal(resp.Status)
  17. }
  18. }
  19. func TestScanRESTfulVerbs(t *testing.T) {
  20. // GET should work
  21. req, _ := http.NewRequest("GET", ts.URL, nil)
  22. data := req.URL.Query()
  23. data.Add("host", "cloudflare.com")
  24. req.URL.RawQuery = data.Encode()
  25. resp, _ := http.DefaultClient.Do(req)
  26. if resp.StatusCode != http.StatusOK {
  27. t.Fatal(resp.Status)
  28. }
  29. // POST, PUT, DELETE, WHATEVER should return 400 errors
  30. req, _ = http.NewRequest("POST", ts.URL, nil)
  31. resp, _ = http.DefaultClient.Do(req)
  32. if resp.StatusCode != http.StatusMethodNotAllowed {
  33. t.Fatal(resp.Status)
  34. }
  35. req, _ = http.NewRequest("DELETE", ts.URL, nil)
  36. resp, _ = http.DefaultClient.Do(req)
  37. if resp.StatusCode != http.StatusMethodNotAllowed {
  38. t.Fatal(resp.Status)
  39. }
  40. req, _ = http.NewRequest("PUT", ts.URL, nil)
  41. resp, _ = http.DefaultClient.Do(req)
  42. if resp.StatusCode != http.StatusMethodNotAllowed {
  43. t.Fatal(resp.Status)
  44. }
  45. req, _ = http.NewRequest("WHATEVER", ts.URL, nil)
  46. resp, _ = http.DefaultClient.Do(req)
  47. if resp.StatusCode != http.StatusMethodNotAllowed {
  48. t.Fatal(resp.Status)
  49. }
  50. }
  51. func TestNewInfoHandler(t *testing.T) {
  52. handler := NewInfoHandler()
  53. if handler == nil {
  54. t.Fatal("Handler error")
  55. }
  56. }