serve_test.go 2.1 KB

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