Exemple #1
0
func TestResponseError_Error_401(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusUnauthorized)
		respondWith(w, `{"message":"Unauthorized"}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Tf(t, strings.Contains(err.Error(), "401 - Unauthorized"), "%s", err.Error())

	e := err.(*ResponseError)
	assert.Equal(t, ErrorUnauthorized, e.Type)

	mux.HandleFunc("/error_2fa", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		head.Set("X-GitHub-OTP", "required; app")
		w.WriteHeader(http.StatusUnauthorized)
		respondWith(w, `{"message":"Unauthorized"}`)
	})

	req, _ = client.NewRequest("error_2fa")
	_, err = req.Get(nil)
	assert.Tf(t, strings.Contains(err.Error(), "401 - Unauthorized"), "%s", err.Error())

	e = err.(*ResponseError)
	assert.Equal(t, ErrorOneTimePasswordRequired, e.Type)
}
Exemple #2
0
func TestSuccessfulGetWithoutOutput(t *testing.T) {
	setup := Setup(t)
	defer setup.Teardown()

	setup.Mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
		assert.Equal(t, "GET", r.Method)
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusOK)
		w.Write([]byte(`{"id": 1, "login": "******"}`))
	})

	client := setup.Client
	user := &TestUser{}

	req, err := client.NewRequest("user")
	assert.Equal(t, nil, err)

	res := req.Get()
	assert.Equal(t, false, res.IsError())
	assert.Equal(t, false, res.IsApiError())

	assert.Tf(t, !res.IsError(), "Response shouldn't have error")
	assert.Equal(t, 200, res.StatusCode)
	assert.Equal(t, false, res.BodyClosed)
	assert.Equal(t, 0, user.Id)
	assert.Equal(t, "", user.Login)

	dec := json.NewDecoder(res.Body)
	dec.Decode(user)
	assert.Equal(t, 1, user.Id)
	assert.Equal(t, "sawyer", user.Login)
}
Exemple #3
0
func TestUsersService_All(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")

		rr := regexp.MustCompile(`users\?since=\d+`)
		assert.Tf(t, rr.MatchString(r.URL.String()), "Regexp should match users?since=\\d+")

		header := w.Header()
		link := fmt.Sprintf(`<%s>; rel="next", <%s>; rel="first"`, testURLOf("users?since=135"), testURLOf("users{?since}"))
		header.Set("Link", link)
		respondWithJSON(w, loadFixture("users.json"))
	})

	url, err := UserURL.Expand(M{"since": 1})
	assert.Equal(t, nil, err)

	q := url.Query()
	q.Set("since", "1")
	url.RawQuery = q.Encode()
	allUsers, result := client.Users(url).All()

	assert.T(t, !result.HasError())
	assert.Equal(t, 1, len(allUsers))
	assert.Equal(t, testURLStringOf("users?since=135"), string(*result.NextPage))

	nextPageURL, err := result.NextPage.Expand(nil)
	assert.Equal(t, nil, err)

	allUsers, result = client.Users(nextPageURL).All()
	assert.T(t, !result.HasError())
	assert.Equal(t, 1, len(allUsers))
}
Exemple #4
0
func TestResponseError_Error_415(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusUnsupportedMediaType)
		respondWith(w, `{"message":"Unsupported Media Type", "documentation_url":"http://developer.github.com/v3"}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Tf(t, strings.Contains(err.Error(), "415 - Unsupported Media Type"), "%s", err.Error())
	assert.Tf(t, strings.Contains(err.Error(), "// See: http://developer.github.com/v3"), "%s", err.Error())

	e := err.(*ResponseError)
	assert.Equal(t, ErrorUnsupportedMediaType, e.Type)
}
Exemple #5
0
func TestResponseError_Error_422_error_summary(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(422)
		respondWith(w, `{"message":"Validation Failed", "errors": [{"resource":"Issue", "field": "title", "code": "missing_field"}]}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Tf(t, strings.Contains(err.Error(), "422 - Validation Failed"), "%s", err.Error())
	assert.Tf(t, strings.Contains(err.Error(), "missing_field error caused by title field on Issue resource"), "%s", err.Error())

	e := err.(*ResponseError)
	assert.Equal(t, ErrorUnprocessableEntity, e.Type)
}
Exemple #6
0
func TestResponseError_Error_422_error(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(422)
		respondWith(w, `{"error":"No repository found for hubtopic"}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Tf(t, strings.Contains(err.Error(), "Error: No repository found for hubtopic"), "%s", err.Error())

	e := err.(*ResponseError)
	assert.Equal(t, ErrorUnprocessableEntity, e.Type)
}
Exemple #7
0
func TestResponseError_Error_400(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		head := w.Header()
		head.Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusBadRequest)
		respondWith(w, `{"message":"Problems parsing JSON"}`)
	})

	req, _ := client.NewRequest("error")
	_, err := req.Get(nil)
	assert.Tf(t, strings.Contains(err.Error(), "400 - Problems parsing JSON"), "%s", err.Error())

	e := err.(*ResponseError)
	assert.Equal(t, ErrorBadRequest, e.Type)
}
Exemple #8
0
func TestGetResponseError(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
		header := w.Header()
		header.Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusNotFound)
		respondWith(w, `{"message": "not found"}`)
	})

	req, err := client.NewRequest("foo")
	assert.Equal(t, nil, err)

	var output map[string]interface{}
	_, err = req.Get(output)
	assert.NotEqual(t, nil, err)
	respErr, ok := err.(*ResponseError)
	assert.Tf(t, ok, "should be able to convert to *ResponseError")
	assert.Equal(t, "not found", respErr.Message)
	assert.Equal(t, ErrorNotFound, respErr.Type)
}
Exemple #9
0
func TestSuccessfulGetWithoutDecoder(t *testing.T) {
	setup := Setup(t)
	defer setup.Teardown()

	setup.Mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
		assert.Equal(t, "GET", r.Method)
		head := w.Header()
		head.Set("Content-Type", "application/booya+booya")
		w.WriteHeader(http.StatusOK)
		w.Write([]byte(`{"id": 1, "login": "******"}`))
	})

	client := setup.Client
	user := &TestUser{}

	req, err := client.NewRequest("user")
	assert.Equal(t, nil, err)

	res := req.Get()
	assert.NotEqual(t, nil, res.Decode(user), "response should have decoder error")
	assert.Tf(t, strings.HasPrefix(res.Error(), "No decoder found for format booya"), "Bad error: %s", res.Error())
}