func (s *S) TestReturnsErrorWhenRequestIsInvalid(c *C) { httpClient := requests.NewHTTPClient("invalid-host") args := requests.Args{Method: "GET", Path: "/path", Body: nil} _, _, _, err := httpClient.MakeRequest(args) _, ok := err.(errors.RequestError) c.Assert(ok, Equals, true) }
func (s *S) TestIncludesHeader(c *C) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { auth := req.Header.Get("Authorization") c.Assert(auth, Equals, "Token abcde") })) defer server.Close() httpClient := requests.NewHTTPClient(server.URL) args := requests.Args{Method: "GET", Path: "/path", Body: nil, Headers: http.Header{"Authorization": {"Token abcde"}}} httpClient.MakeRequest(args) }
func (s *S) TestMakeRequest(c *C) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte(`{"name": "Alice"}`)) })) defer server.Close() httpClient := requests.NewHTTPClient(server.URL) args := requests.Args{Method: "GET", Path: "/path", Body: nil, AcceptableCode: http.StatusOK} _, _, body, err := httpClient.MakeRequest(args) c.Assert(string(body), Equals, `{"name": "Alice"}`) c.Check(err, IsNil) }
func (s *S) TestReturnsErrorWhenResponseIsInvalid(c *C) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Length", "1") w.Write([]byte("{}")) })) defer server.Close() httpClient := requests.NewHTTPClient(server.URL) args := requests.Args{Method: "GET", Path: "/path", Body: nil} _, _, _, err := httpClient.MakeRequest(args) _, ok := err.(errors.ResponseError) c.Assert(ok, Equals, true) }
func (s *S) TestReturnsDefaultError(c *C) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusBadRequest) w.Write([]byte(`{}`)) })) defer server.Close() httpClient := requests.NewHTTPClient(server.URL) args := requests.Args{Method: "GET", Path: "/path", Body: nil} _, _, _, err := httpClient.MakeRequest(args) e, ok := err.(errors.ResponseError) c.Assert(e.Error(), Equals, "The response was invalid or cannot be served.") c.Assert(ok, Equals, true) }
func (s *S) TestMakeRequestWithNonAcceptableCode(c *C) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte(`{"name": "Alice"}`)) })) defer server.Close() httpClient := requests.NewHTTPClient(server.URL) args := requests.Args{Method: "GET", Path: "/path", Body: nil, AcceptableCode: http.StatusBadRequest} _, _, body, err := httpClient.MakeRequest(args) c.Assert(string(body), Equals, `{"name": "Alice"}`) e, ok := err.(errors.ResponseError) c.Assert(ok, Equals, true) c.Assert(e.Error(), Equals, "The response was invalid or cannot be served.") }
func sendWebHook(config account.HookConfig, body interface{}) { if config.Address != "" { url, err := url.Parse(config.Address) if config.Method == "" { config.Method = "POST" } b := body.([]byte) httpClient := requests.NewHTTPClient(fmt.Sprintf("%s://%s", url.Scheme, url.Host)) _, _, _, err = httpClient.MakeRequest(requests.Args{ AcceptableCode: http.StatusOK, Method: config.Method, Path: url.RequestURI(), Body: string(b), }) if err != nil { Logger.Warn(fmt.Sprintf("Failed to call WebHook for %s: %s.", config.Address, err.Error())) } } }
func (s *S) SetUpTest(c *C) { // setUpMongoreTest(s) setUpMemoryTest(s) s.api = api.NewApi(s.store, s.pubsub) s.server = httptest.NewServer(s.api.Handler()) httpClient = requests.NewHTTPClient(s.server.URL) team = account.Team{Name: "ApiHub Team", Alias: "apihub"} service = account.Service{Endpoint: "http://example.org/api", Subdomain: "apihub"} user = account.User{Name: "Bob", Email: "*****@*****.**", Password: "******"} app = account.App{ClientId: "ios", ClientSecret: "secret", Name: "Ios App", Team: team.Alias, Owner: user.Email, RedirectUris: []string{"http://www.example.org/auth"}} pluginConfig = account.Plugin{Name: "Plugin Config", Service: service.Subdomain, Config: map[string]interface{}{"version": 1}} user.Create() token, err := s.api.Login(user.Email, "secret") if err != nil { panic(err) } s.authHeader = fmt.Sprintf("%s %s", token.Type, token.AccessToken) }
func (s *S) SetUpTest(c *C) { s.ng = memory.New() s.api = api.NewApi(s.ng) s.server = httptest.NewServer(s.api.Handler()) httpClient = requests.NewHTTPClient(s.server.URL) }