server_unit_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package server
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. "github.com/docker/docker/api"
  10. "github.com/docker/docker/engine"
  11. "github.com/docker/docker/pkg/version"
  12. )
  13. func TestHttpError(t *testing.T) {
  14. r := httptest.NewRecorder()
  15. httpError(r, fmt.Errorf("No such method"))
  16. if r.Code != http.StatusNotFound {
  17. t.Fatalf("Expected %d, got %d", http.StatusNotFound, r.Code)
  18. }
  19. r = httptest.NewRecorder()
  20. httpError(r, fmt.Errorf("This accound hasn't been activated"))
  21. if r.Code != http.StatusForbidden {
  22. t.Fatalf("Expected %d, got %d", http.StatusForbidden, r.Code)
  23. }
  24. r = httptest.NewRecorder()
  25. httpError(r, fmt.Errorf("Some error"))
  26. if r.Code != http.StatusInternalServerError {
  27. t.Fatalf("Expected %d, got %d", http.StatusInternalServerError, r.Code)
  28. }
  29. }
  30. func TestGetImagesByName(t *testing.T) {
  31. eng := engine.New()
  32. name := "image_name"
  33. var called bool
  34. eng.Register("image_inspect", func(job *engine.Job) error {
  35. called = true
  36. if job.Args[0] != name {
  37. t.Fatalf("name != '%s': %#v", name, job.Args[0])
  38. }
  39. if api.APIVERSION.LessThan("1.12") && !job.GetenvBool("dirty") {
  40. t.Fatal("dirty env variable not set")
  41. } else if api.APIVERSION.GreaterThanOrEqualTo("1.12") && job.GetenvBool("dirty") {
  42. t.Fatal("dirty env variable set when it shouldn't")
  43. }
  44. v := &engine.Env{}
  45. v.SetBool("dirty", true)
  46. if _, err := v.WriteTo(job.Stdout); err != nil {
  47. return err
  48. }
  49. return nil
  50. })
  51. r := serveRequest("GET", "/images/"+name+"/json", nil, eng, t)
  52. if !called {
  53. t.Fatal("handler was not called")
  54. }
  55. if r.HeaderMap.Get("Content-Type") != "application/json" {
  56. t.Fatalf("%#v\n", r)
  57. }
  58. var stdoutJson interface{}
  59. if err := json.Unmarshal(r.Body.Bytes(), &stdoutJson); err != nil {
  60. t.Fatalf("%#v", err)
  61. }
  62. if stdoutJson.(map[string]interface{})["dirty"].(float64) != 1 {
  63. t.Fatalf("%#v", stdoutJson)
  64. }
  65. }
  66. func serveRequest(method, target string, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
  67. return serveRequestUsingVersion(method, target, api.APIVERSION, body, eng, t)
  68. }
  69. func serveRequestUsingVersion(method, target string, version version.Version, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
  70. r := httptest.NewRecorder()
  71. req, err := http.NewRequest(method, target, body)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. ServeRequest(eng, version, r, req)
  76. return r
  77. }
  78. func readEnv(src io.Reader, t *testing.T) *engine.Env {
  79. out := engine.NewOutput()
  80. v, err := out.AddEnv()
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. if _, err := io.Copy(out, src); err != nil {
  85. t.Fatal(err)
  86. }
  87. out.Close()
  88. return v
  89. }
  90. func assertContentType(recorder *httptest.ResponseRecorder, contentType string, t *testing.T) {
  91. if recorder.HeaderMap.Get("Content-Type") != contentType {
  92. t.Fatalf("%#v\n", recorder)
  93. }
  94. }