github.com/IBM-Bluemix/golang-openssl-wrapper@v0.0.0-20160104220506-7f2d5273b515/ssl/httpsserver_test.go (about)

     1  package ssl_test
     2  
     3  import (
     4  	. "github.com/IBM-Bluemix/golang-openssl-wrapper/ssl"
     5  
     6  	"os"
     7  	"os/exec"
     8  	"time"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var c *exec.Cmd
    15  var _ = BeforeSuite(compileAndStartServer)
    16  var _ = AfterSuite(cleanup)
    17  
    18  var _ = Describe("Httpsserver", func() {
    19  	It("Should get a valid response from the /aloha endpoint", func() {
    20  		client := NewHTTPSClient()
    21  		url := "https://localhost:8443/aloha"
    22  		res, e := client.Get(url)
    23  		Expect(e).To(BeNil())
    24  		Expect(res).NotTo(BeNil())
    25  
    26  		body := make([]byte, 200)
    27  		i, e := res.Body.Read(body)
    28  		Expect(e).To(BeNil())
    29  		Expect(i).To(BeNumerically(">", 0))
    30  		Expect(string(body[:i])).To(Equal("ALOHA!!"))
    31  		Expect(res.Body.Close()).To(BeNil())
    32  	})
    33  
    34  	It("Should get a valid response from the /server endpoint", func() {
    35  		client := NewHTTPSClient()
    36  		url := "https://localhost:8443/server"
    37  		res, e := client.Get(url)
    38  		Expect(e).To(BeNil())
    39  		Expect(res).NotTo(BeNil())
    40  
    41  		Expect(res.Header.Get("Server")).To(Equal("https://github.com/IBM-Bluemix/golang-openssl-wrapper"))
    42  		Expect(res.Body.Close()).To(BeNil())
    43  	})
    44  
    45  	It("Should get a valid response from the /mux endpoint", func() {
    46  		client := NewHTTPSClient()
    47  		url := "https://localhost:8443/mux"
    48  		res, e := client.Get(url)
    49  		Expect(e).To(BeNil())
    50  		Expect(res).NotTo(BeNil())
    51  
    52  		body := make([]byte, 200)
    53  		i, e := res.Body.Read(body)
    54  		Expect(e).To(BeNil())
    55  		Expect(i).To(BeNumerically(">", 0))
    56  		Expect(string(body[:i])).To(Equal("Using gorilla/mux"))
    57  		Expect(res.Body.Close()).To(BeNil())
    58  	})
    59  })
    60  
    61  func cleanup() {
    62  	c.Process.Kill()
    63  }
    64  
    65  func compileAndStartServer() {
    66  	check := func(e error) {
    67  		if e != nil {
    68  			panic(e)
    69  		}
    70  	}
    71  
    72  	check(os.Chdir("tests"))
    73  
    74  	// Compile HTTPSServer
    75  	c = exec.Command("go", "build", "httpsserver.go")
    76  	check(c.Start())
    77  	check(c.Wait())
    78  
    79  	// Run HTTPSServer
    80  	c = exec.Command("./httpsserver")
    81  	check(c.Start())
    82  
    83  	time.Sleep(2 * time.Second)
    84  }