github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/droplet_runner/proxyconf_reader_test.go (about)

     1  package droplet_runner_test
     2  
     3  import (
     4  	"net/http"
     5  	"reflect"
     6  
     7  	"github.com/cloudfoundry-incubator/ltc/droplet_runner"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/onsi/gomega/ghttp"
    11  )
    12  
    13  var _ = Describe("HTTPProxyConfReader", func() {
    14  	var (
    15  		proxyConfReader *droplet_runner.HTTPProxyConfReader
    16  		fakeServer      *ghttp.Server
    17  		badURL          string
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		fakeServer = ghttp.NewServer()
    22  
    23  		badServer := ghttp.NewServer()
    24  		badURL = badServer.URL()
    25  		badServer.Close()
    26  
    27  		proxyConfReader = &droplet_runner.HTTPProxyConfReader{
    28  			URL: fakeServer.URL() + "/pc.json",
    29  		}
    30  	})
    31  
    32  	AfterEach(func() {
    33  		if fakeServer != nil {
    34  			fakeServer.Close()
    35  		}
    36  	})
    37  
    38  	Context("#ProxyConf", func() {
    39  		It("should parse JSON on 200", func() {
    40  			fakeServer.RouteToHandler("GET", "/pc.json", ghttp.CombineHandlers(
    41  				ghttp.RespondWith(200,
    42  					`{"http_proxy": "http://proxy", "https_proxy": "https://proxy", "no_proxy": "no-proxy"}`,
    43  					http.Header{"Content-Type": []string{"application/json"}},
    44  				),
    45  			))
    46  
    47  			proxyConf, err := proxyConfReader.ProxyConf()
    48  			Expect(err).NotTo(HaveOccurred())
    49  
    50  			Expect(proxyConf.HTTPProxy).To(Equal("http://proxy"))
    51  			Expect(proxyConf.HTTPSProxy).To(Equal("https://proxy"))
    52  			Expect(proxyConf.NoProxy).To(Equal("no-proxy"))
    53  
    54  			Expect(fakeServer.ReceivedRequests()).To(HaveLen(1))
    55  		})
    56  
    57  		It("should fail when it receives invalid JSON on 200", func() {
    58  			fakeServer.RouteToHandler("GET", "/pc.json", ghttp.CombineHandlers(
    59  				ghttp.RespondWith(200, `{`, http.Header{"Content-Type": []string{"application/json"}}),
    60  			))
    61  
    62  			_, err := proxyConfReader.ProxyConf()
    63  			Expect(err).To(MatchError("unexpected end of JSON input"))
    64  
    65  			Expect(fakeServer.ReceivedRequests()).To(HaveLen(1))
    66  		})
    67  
    68  		It("should return an empty ProxyConf on 404", func() {
    69  			fakeServer.RouteToHandler("GET", "/pc.json", ghttp.CombineHandlers(
    70  				ghttp.RespondWith(404, "/pc.json not found"),
    71  			))
    72  
    73  			proxyConf, err := proxyConfReader.ProxyConf()
    74  			Expect(err).NotTo(HaveOccurred())
    75  
    76  			Expect(proxyConf.HTTPProxy).To(BeEmpty())
    77  			Expect(proxyConf.HTTPSProxy).To(BeEmpty())
    78  			Expect(proxyConf.NoProxy).To(BeEmpty())
    79  
    80  			Expect(fakeServer.ReceivedRequests()).To(HaveLen(1))
    81  		})
    82  
    83  		It("should return an error on any other HTTP status", func() {
    84  			fakeServer.RouteToHandler("GET", "/pc.json", ghttp.CombineHandlers(
    85  				ghttp.RespondWith(500, "fail"),
    86  			))
    87  
    88  			_, err := proxyConfReader.ProxyConf()
    89  			Expect(err).To(MatchError("500 Internal Server Error"))
    90  
    91  			Expect(fakeServer.ReceivedRequests()).To(HaveLen(1))
    92  		})
    93  
    94  		It("should return an error on any other HTTP status", func() {
    95  			proxyConfReader.URL = badURL
    96  
    97  			_, err := proxyConfReader.ProxyConf()
    98  			Expect(reflect.TypeOf(err).String()).To(Equal("*url.Error"))
    99  			Expect(fakeServer.ReceivedRequests()).To(HaveLen(0))
   100  		})
   101  	})
   102  })