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