Пример #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)
}
Пример #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)
}
Пример #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)
}
Пример #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)
}
Пример #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)
}
Пример #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.")
}
Пример #7
0
func sendWebHook(config account.HookConfig, body interface{}) {
	if config.Address != "" {
		if config.Method == "" {
			config.Method = "POST"
		}
		httpClient := requests.NewHTTPClient(config.Address)
		_, _, _, err := httpClient.MakeRequest(requests.Args{
			AcceptableCode: http.StatusOK,
			Method:         config.Method,
			Body:           body,
		})

		if err != nil {
			Logger.Warn(fmt.Sprintf("Failed to call WebHook for %s: %s.", config.Address, err.Error()))
		}
	}
}
Пример #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: "Backstage Team", Alias: "backstage"}
	service = account.Service{Endpoint: "http://example.org/api", Subdomain: "backstage"}
	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)
}