serve_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package serve
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "github.com/cloudflare/cfssl/cli"
  7. )
  8. func TestServe(t *testing.T) {
  9. registerHandlers()
  10. ts := httptest.NewServer(http.DefaultServeMux)
  11. defer ts.Close()
  12. expected := make(map[string]int)
  13. for endpoint := range endpoints {
  14. expected[v1APIPath(endpoint)] = http.StatusOK
  15. }
  16. // Disabled endpoints should return '404 Not Found'
  17. expected[v1APIPath("sign")] = http.StatusNotFound
  18. expected[v1APIPath("authsign")] = http.StatusNotFound
  19. expected[v1APIPath("newcert")] = http.StatusNotFound
  20. expected[v1APIPath("info")] = http.StatusNotFound
  21. expected[v1APIPath("ocspsign")] = http.StatusNotFound
  22. expected[v1APIPath("crl")] = http.StatusNotFound
  23. expected[v1APIPath("gencrl")] = http.StatusNotFound
  24. expected[v1APIPath("revoke")] = http.StatusNotFound
  25. // Enabled endpoints should return '405 Method Not Allowed'
  26. expected[v1APIPath("init_ca")] = http.StatusMethodNotAllowed
  27. expected[v1APIPath("newkey")] = http.StatusMethodNotAllowed
  28. expected[v1APIPath("bundle")] = http.StatusMethodNotAllowed
  29. expected[v1APIPath("certinfo")] = http.StatusMethodNotAllowed
  30. expected[v1APIPath("certadd")] = http.StatusMethodNotAllowed
  31. // POST-only endpoints should return '400 Bad Request'
  32. expected[v1APIPath("scan")] = http.StatusBadRequest
  33. // Redirected HTML endpoints should return '200 OK'
  34. expected["/scan"] = http.StatusOK
  35. expected["/bundle"] = http.StatusOK
  36. // Non-existent endpoints should return '404 Not Found'
  37. expected["/bad_endpoint"] = http.StatusNotFound
  38. for endpoint, status := range expected {
  39. resp, err := http.Get(ts.URL + endpoint)
  40. if err != nil {
  41. t.Error(err)
  42. }
  43. if resp.StatusCode != status {
  44. t.Fatalf("%s: '%s' (expected '%s')", endpoint, resp.Status, http.StatusText(status))
  45. }
  46. }
  47. var c cli.Config
  48. var test = []string{"test"}
  49. if err := serverMain(test, c); err == nil {
  50. t.Fatalf("There should be an error for argument")
  51. }
  52. }