Exemple #1
0
func TestQuote(t *testing.T) {
	soapServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, `<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
       <m:GetLastTradePriceResponse xmlns:m="Some-URI">
           <Price>34.5</Price>
       </m:GetLastTradePriceResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>`)
	}))

	defer soapServer.Close()

	soapURL, _ := url.Parse(soapServer.URL)

	wrapped := quote.Middleware(customctx.ContextHandlerFunc(quote.NewQuoteHandler(soapURL.Host)))
	wrapped = timing.TimerMiddleware(wrapped)

	h := &customctx.ContextAdapter{
		Ctx:     context.Background(),
		Handler: wrapped,
	}

	ts := httptest.NewServer(h)
	defer ts.Close()

	req, err := http.NewRequest("GET", ts.URL+"/quote/MSFT", nil)
	if err != nil {
		log.Fatal(err.Error())
	}

	client := http.Client{}

	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err.Error())
	}

	body, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()
	if err != nil {
		log.Fatal(err.Error())
	}

	println(string(body))

	if !strings.Contains(string(body), "34.5") {
		t.Fail()
	}
}
Exemple #2
0
func main() {
	var endpoint = "mb:4545"

	endPointFromEnv := os.Getenv("QUOTE_ENDPOINT")
	if endPointFromEnv != "" {
		endpoint = endPointFromEnv
		fmt.Println("Using endpoint from environment:", endpoint)
	}

	fmt.Println("starting wrapped quote at ", endpoint)
	wrapped := quote.Middleware(customctx.ContextHandlerFunc(quote.NewQuoteHandler(endpoint)))
	wrapped = timing.TimerMiddleware(wrapped)

	h := &customctx.ContextAdapter{
		Ctx:     context.Background(),
		Handler: wrapped,
	}
	http.ListenAndServe(":8080", h)
}
Exemple #3
0
func TestWithXRequestID(t *testing.T) {
	h := &customctx.ContextAdapter{
		Ctx:     context.Background(),
		Handler: timing.TimerMiddleware(reqid.RequestIdMiddleware(customctx.ContextHandlerFunc(handler))),
	}

	ts := httptest.NewServer(h)
	defer ts.Close()

	req, err := http.NewRequest("GET", ts.URL, nil)
	if err != nil {
		log.Fatal(err.Error())
	}

	req.Header.Set("X-Request-ID", "request-id")

	client := http.Client{}
	var resp *http.Response

	for i := 0; i < 1; i++ {
		resp, err = client.Do(req)
		if err != nil {
			log.Fatal(err.Error())
		}
	}

	body, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()
	if err != nil {
		log.Fatal(err.Error())
	}

	if !strings.Contains(string(body), "request-id") {
		t.Fail()
	}
}