You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

151 lines
4.1 KiB

  1. // Copyright 2015 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package har
  15. import (
  16. "encoding/json"
  17. "net/http"
  18. "net/http/httptest"
  19. "testing"
  20. "github.com/google/martian"
  21. "github.com/google/martian/proxyutil"
  22. )
  23. func TestExportHandlerServeHTTP(t *testing.T) {
  24. logger := NewLogger()
  25. req, err := http.NewRequest("GET", "http://example.com", nil)
  26. if err != nil {
  27. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  28. }
  29. _, remove, err := martian.TestContext(req, nil, nil)
  30. if err != nil {
  31. t.Fatalf("martian.TestContext(): got %v, want no error", err)
  32. }
  33. defer remove()
  34. if err := logger.ModifyRequest(req); err != nil {
  35. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  36. }
  37. res := proxyutil.NewResponse(200, nil, req)
  38. if err := logger.ModifyResponse(res); err != nil {
  39. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  40. }
  41. h := NewExportHandler(logger)
  42. req, err = http.NewRequest("GET", "/", nil)
  43. if err != nil {
  44. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  45. }
  46. rw := httptest.NewRecorder()
  47. h.ServeHTTP(rw, req)
  48. if got, want := rw.Code, http.StatusOK; got != want {
  49. t.Errorf("rw.Code: got %d, want %d", got, want)
  50. }
  51. hl := &HAR{}
  52. if err := json.Unmarshal(rw.Body.Bytes(), hl); err != nil {
  53. t.Fatalf("json.Unmarshal(): got %v, want no error", err)
  54. }
  55. if got, want := len(hl.Log.Entries), 1; got != want {
  56. t.Fatalf("len(hl.Log.Entries): got %v, want %v", got, want)
  57. }
  58. entry := hl.Log.Entries[0]
  59. if got, want := entry.Request.URL, "http://example.com"; got != want {
  60. t.Errorf("Request.URL: got %q, want %q", got, want)
  61. }
  62. if got, want := entry.Response.Status, 200; got != want {
  63. t.Errorf("Response.Status: got %d, want %d", got, want)
  64. }
  65. rh := NewResetHandler(logger)
  66. req, err = http.NewRequest("DELETE", "/", nil)
  67. if err != nil {
  68. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  69. }
  70. rw = httptest.NewRecorder()
  71. rh.ServeHTTP(rw, req)
  72. req, err = http.NewRequest("GET", "/", nil)
  73. if err != nil {
  74. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  75. }
  76. rw = httptest.NewRecorder()
  77. h.ServeHTTP(rw, req)
  78. if got, want := rw.Code, http.StatusOK; got != want {
  79. t.Errorf("rw.Code: got %d, want %d", got, want)
  80. }
  81. hl = &HAR{}
  82. if err := json.Unmarshal(rw.Body.Bytes(), hl); err != nil {
  83. t.Fatalf("json.Unmarshal(): got %v, want no error", err)
  84. }
  85. if got, want := len(hl.Log.Entries), 0; got != want {
  86. t.Errorf("len(Log.Entries): got %v, want %v", got, want)
  87. }
  88. req, err = http.NewRequest("DELETE", "/?return=1", nil)
  89. if err != nil {
  90. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  91. }
  92. rw = httptest.NewRecorder()
  93. rh.ServeHTTP(rw, req)
  94. if got, want := rw.Code, http.StatusOK; got != want {
  95. t.Errorf("rw.Code: got %d, want %d", got, want)
  96. }
  97. hl = &HAR{}
  98. if err := json.Unmarshal(rw.Body.Bytes(), hl); err != nil {
  99. t.Fatalf("json.Unmarshal(): got %v, want no error", err)
  100. }
  101. if got, want := len(hl.Log.Entries), 0; got != want {
  102. t.Errorf("len(Log.Entries): got %v, want %v", got, want)
  103. }
  104. req, err = http.NewRequest("DELETE", "/?return=0", nil)
  105. if err != nil {
  106. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  107. }
  108. rw = httptest.NewRecorder()
  109. rh.ServeHTTP(rw, req)
  110. if got, want := rw.Code, http.StatusNoContent; got != want {
  111. t.Errorf("rw.Code: got %d, want %d", got, want)
  112. }
  113. req, err = http.NewRequest("DELETE", "/?return=notboolean", nil)
  114. if err != nil {
  115. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  116. }
  117. rw = httptest.NewRecorder()
  118. rh.ServeHTTP(rw, req)
  119. if got, want := rw.Code, http.StatusBadRequest; got != want {
  120. t.Errorf("rw.Code: got %d, want %d", got, want)
  121. }
  122. }