github.com/IBM-Bluemix/golang-openssl-wrapper@v0.0.0-20160104220506-7f2d5273b515/ssl/httpsclient_test.go (about) 1 package ssl_test 2 3 import ( 4 . "github.com/IBM-Bluemix/golang-openssl-wrapper/ssl" 5 6 "fmt" 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 "io/ioutil" 10 "net/http" 11 // "net/url" 12 "net" 13 "strings" 14 ) 15 16 var _ = Describe("Httpsclient", func() { 17 18 var t *http.Transport 19 var h HTTPSConn 20 var host, resource string 21 var respLen int 22 var port, ua, dest, requestContent string 23 // var server *httptest.Server 24 25 BeforeEach(func() { 26 host = "www.random.org" 27 respLen = 8 28 resource = fmt.Sprintf("/strings/?num=1&len=%d&digits=on&upperalpha=on&loweralpha=on&unique=on&format=plain&rnd=new", respLen) 29 }) 30 31 // AfterEach(func() { 32 // server.Close() 33 // }) 34 35 Context("Using the golang http.Client", func() { 36 It("Should fetch a resource successfully", func() { 37 client := NewHTTPSClient() 38 urlPath := "https://" + host + resource 39 response, err := client.Get(urlPath) 40 Expect(err).To(BeNil()) 41 42 defer response.Body.Close() 43 body, _ := ioutil.ReadAll(response.Body) 44 bstring := strings.Trim(string(body), "\n") 45 Expect(len(bstring)).To(Equal(respLen)) 46 }) 47 }) 48 49 Context("Working directly with the underlying Transport", func() { 50 BeforeEach(func() { 51 port = "443" 52 ua = "https://github.com/IBM-Bluemix/golang-openssl-wrapper" 53 /* Fetch a single 8 character string in plaintext format */ 54 requestContent = strings.Join([]string{ 55 fmt.Sprintf("GET %s HTTP/1.1", resource), 56 fmt.Sprintf("User-Agent: %s", ua), 57 fmt.Sprintf("Host: %s", host), 58 "Accept: */*", 59 "\r\n", 60 }, "\r\n") 61 dest = host + ":" + port 62 63 t = NewHTTPSTransport(nil) 64 Expect(t).NotTo(BeNil()) 65 conn, err := t.Dial("tcp", dest) 66 Expect(err).NotTo(HaveOccurred()) 67 h = conn.(HTTPSConn) 68 }) 69 70 Context("Using a bogus hostname and/or IP address", func() { 71 var ip string 72 JustBeforeEach(func() { 73 /* Be sure your internal DNS isn't actually the SOA for ".bogus" */ 74 host = "bogushost.bogus" 75 dest = host + ":" + port 76 /* Change this to an IP which cannot be resolved, which may depend on your specific network configuration */ 77 ip = "10.100.10.100" 78 }) 79 80 It("Errors when the hostname is unresolveable", func() { 81 conn, err := t.Dial("tcp", dest) 82 Expect(conn).To(BeNil()) 83 Expect(err).To(HaveOccurred()) 84 }) 85 86 It("Errors when the hostname is malformed", func() { 87 baddest := dest + ":" 88 conn, err := t.Dial("tcp", baddest) 89 Expect(conn).To(BeNil()) 90 Expect(err).To(HaveOccurred()) 91 }) 92 }) 93 94 It("Should return the correct remote address", func() { 95 match := false 96 /* Build a list of the addresses for host */ 97 addrs, err := net.LookupHost(host) 98 Expect(len(addrs)).To(BeNumerically(">=", 1)) 99 Expect(err).NotTo(HaveOccurred()) 100 101 ra, _, _ := net.SplitHostPort(h.RemoteAddr().String()) 102 103 for _, a := range addrs { 104 if a == ra { 105 match = true 106 } 107 } 108 Expect(match).To(Equal(true)) 109 }) 110 111 It("Should error for an invalid network type", func() { 112 conn, err := t.Dial("bogus", "www.google.com:443") 113 Expect(conn).To(BeNil()) 114 Expect(err).To(HaveOccurred()) 115 }) 116 117 Context("Performing socket I/O", func() { 118 AfterEach(func() { 119 h.Close() 120 }) 121 122 It("Should write to the connection and read the response", func() { 123 wb := []byte(requestContent) 124 Expect(h.Write(wb)).To(BeNumerically(">=", len(wb))) 125 rb := make([]byte, 50) 126 Expect(h.Read(rb)).To(BeNumerically(">", 0)) 127 }) 128 }) 129 130 Context("Connection management", func() { 131 It("Should not allow closing of an already closed connection", func() { 132 h.Close() 133 Expect(h.Close()).NotTo(Succeed()) 134 }) 135 136 }) 137 138 // TODO: use these tests when Set[{Read,Write}]Deadline is implemented 139 // Context("Setting deadlines", func() { 140 // var now time.Time 141 // BeforeEach(func() { 142 // now = time.Now() 143 // }) 144 145 // It("Should not allow setting a deadline equal or or before the current time", func() { 146 // bogus := now.Add(time.Duration(10) * time.Second * (-1)) 147 // Expect(h.SetDeadLine(bogus)).NotTo(Succeed()) 148 // Expect(h.SetReadDeadLine(bogus)).NotTo(Succeed()) 149 // Expect(h.SetWriteDeadLine(bogus)).NotTo(Succeed()) 150 // }) 151 152 // It("Should not allow setting a deadline more than ten (10) minutes in the future", func() { 153 // bogus := now.Add(time.Duration(11) * time.Minute) 154 // Expect(h.SetDeadLine(bogus)).NotTo(Succeed()) 155 // Expect(h.SetReadDeadLine(bogus)).NotTo(Succeed()) 156 // Expect(h.SetWriteDeadLine(bogus)).NotTo(Succeed()) 157 // }) 158 159 // // TODO: Specs for checking that deadlines, having been set, are observed 160 // }) 161 }) 162 })