Exemplo n.º 1
0
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)
}
Exemplo n.º 2
0
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)
}
Exemplo n.º 3
0
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)
}
Exemplo n.º 4
0
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)
}
Exemplo n.º 5
0
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)
}
Exemplo n.º 6
0
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.")
}
Exemplo n.º 7
0
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()))
		}
	}
}
Exemplo n.º 8
0
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)
}
Exemplo n.º 9
0
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)
}