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.
 
 
 

63 lines
1.9 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 martianhttp
  15. import (
  16. "crypto/x509"
  17. "encoding/pem"
  18. "net/http"
  19. "net/http/httptest"
  20. "testing"
  21. "time"
  22. "github.com/google/martian/mitm"
  23. )
  24. func TestAuthorityHandler(t *testing.T) {
  25. ca, _, err := mitm.NewAuthority("martian.proxy", "Martian Authority", time.Hour)
  26. if err != nil {
  27. t.Fatalf("mitm.NewAuthority(): got %v, want no error", err)
  28. }
  29. rw := httptest.NewRecorder()
  30. req, err := http.NewRequest("GET", "/martian/authority.cer", nil)
  31. if err != nil {
  32. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  33. }
  34. h := NewAuthorityHandler(ca)
  35. h.ServeHTTP(rw, req)
  36. if got, want := rw.Code, 200; got != want {
  37. t.Errorf("rw.Code: got %d, want %d", got, want)
  38. }
  39. if got, want := rw.Header().Get("Content-Type"), "application/x-x509-ca-cert"; got != want {
  40. t.Errorf("rw.Header().Get(%q): got %q, want %q", "Content-Type", got, want)
  41. }
  42. blk, _ := pem.Decode(rw.Body.Bytes())
  43. if got, want := blk.Type, "CERTIFICATE"; got != want {
  44. t.Errorf("rw.Body: got PEM type %q, want %q", got, want)
  45. }
  46. cert, err := x509.ParseCertificate(blk.Bytes)
  47. if err != nil {
  48. t.Fatalf("x509.ParseCertificate(res.Body): got %v, want no error", err)
  49. }
  50. if got, want := cert.Subject.CommonName, "martian.proxy"; got != want {
  51. t.Errorf("cert.Subject.CommonName: got %q, want %q", got, want)
  52. }
  53. }