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.
 
 
 

66 regels
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 header
  15. import (
  16. "net/http"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestBadFramingMultipleContentLengths(t *testing.T) {
  21. m := NewBadFramingModifier()
  22. req, err := http.NewRequest("GET", "/", nil)
  23. if err != nil {
  24. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  25. }
  26. req.Header["Content-Length"] = []string{"42", "42, 42"}
  27. if err := m.ModifyRequest(req); err != nil {
  28. t.Errorf("ModifyRequest(): got %v, want no error", err)
  29. }
  30. if got, want := req.Header["Content-Length"], []string{"42"}; !reflect.DeepEqual(got, want) {
  31. t.Errorf("req.Header[%q]: got %v, want %v", "Content-Length", got, want)
  32. }
  33. req.Header["Content-Length"] = []string{"42", "32, 42"}
  34. if err := m.ModifyRequest(req); err == nil {
  35. t.Error("ModifyRequest(): got nil, want error")
  36. }
  37. }
  38. func TestBadFramingTransferEncodingAndContentLength(t *testing.T) {
  39. m := NewBadFramingModifier()
  40. req, err := http.NewRequest("GET", "/", nil)
  41. if err != nil {
  42. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  43. }
  44. req.Header["Transfer-Encoding"] = []string{"gzip, chunked"}
  45. req.Header["Content-Length"] = []string{"42"}
  46. if err := m.ModifyRequest(req); err != nil {
  47. t.Errorf("ModifyRequest(): got %v, want no error", err)
  48. }
  49. if _, ok := req.Header["Content-Length"]; ok {
  50. t.Fatalf("req.Header[%q]: got ok, want !ok", "Content-Length")
  51. }
  52. req.Header.Set("Transfer-Encoding", "gzip, identity")
  53. req.Header.Del("Content-Length")
  54. if err := m.ModifyRequest(req); err == nil {
  55. t.Error("ModifyRequest(): got nil, want error")
  56. }
  57. }