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.
 
 
 

79 lines
2.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 status
  15. import (
  16. "fmt"
  17. "net/http"
  18. "testing"
  19. "github.com/google/martian/parse"
  20. "github.com/google/martian/proxyutil"
  21. )
  22. func TestFromJSON(t *testing.T) {
  23. msg := []byte(`{
  24. "status.Modifier": {
  25. "scope": ["response"],
  26. "statusCode": 400
  27. }
  28. }`)
  29. r, err := parse.FromJSON(msg)
  30. if err != nil {
  31. t.Fatalf("parse.FromJSON(): got %v, want no error", err)
  32. }
  33. resmod := r.ResponseModifier()
  34. if resmod == nil {
  35. t.Fatal("resmod: got nil, want not nil")
  36. }
  37. res := proxyutil.NewResponse(200, nil, nil)
  38. if err := resmod.ModifyResponse(res); err != nil {
  39. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  40. }
  41. if got, want := res.StatusCode, 400; got != want {
  42. t.Errorf("res.StatusCode: got %d, want %d", got, want)
  43. }
  44. }
  45. func TestStatusModifierOnResponse(t *testing.T) {
  46. for i, status := range []int{
  47. http.StatusForbidden,
  48. http.StatusOK,
  49. http.StatusTemporaryRedirect,
  50. } {
  51. req, err := http.NewRequest("GET", "/", nil)
  52. if err != nil {
  53. t.Fatalf("NewRequest(): got %v, want no error", err)
  54. }
  55. res := proxyutil.NewResponse(200, nil, req)
  56. mod := NewModifier(status)
  57. if err := mod.ModifyResponse(res); err != nil {
  58. t.Fatalf("%d. ModifyResponse(): got %v, want no error", i, err)
  59. }
  60. if got, want := res.StatusCode, status; got != want {
  61. t.Errorf("%d. res.StatusCode: got %v, want %v", i, got, want)
  62. }
  63. if got, want := res.Status, fmt.Sprintf("%d %s", res.StatusCode, http.StatusText(status)); got != want {
  64. t.Errorf("%d. res.Status: got %q, want %q", i, got, want)
  65. }
  66. }
  67. }