server_unit_test.go 2.7 KB

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