Пример #1
0
func TestServePostByIDWithInvalidID(t *testing.T) {

	//Creates a request with an invalid ID
	r, err := http.NewRequest("GET", "api/posts/5975ea52-2a91-483f-b52f-2b0257886773", nil)
	if err != nil {
		t.Error(err)
	}
	w := httptest.NewRecorder()

	ServePostByID(new(MockQuestionStore))(m.NewContext(), w, r)

	if w.Code != http.StatusBadRequest {
		t.Errorf("Expected a status code of 400, but recieved an http status code of %d", w.Code)
	} else if w.Body.String() != "No question exists with the provided id\n" {
		t.Errorf("Expected the content of the responsewriter to be \"No question exists with the provided id\", but instead the responsewriter contains %s", w.Body.String())
	}
}
Пример #2
0
func TestServeSortedQuestions(t *testing.T) {

	//Creates a request with filters that no questions satisfy
	r, err := http.NewRequest("GET", "api/questions/upvotes/desc/10", nil)
	if err != nil {
		t.Error(err)
	}

	w := httptest.NewRecorder()

	ServeSortedQuestions(new(MockQuestionStore))(m.NewContext(), w, r)

	if w.Code != http.StatusBadRequest {
		t.Errorf("Expected a status code of 400, because the MockQuestionStore's FindQuestionsByFilter method always returns nil as a result, but recieved an http status code of %d", w.Code)
	} else if w.Body.String() != "No questions match the specifications in the url\n" {
		t.Errorf("Expected the content of the responsewriter to be \"No questions match the specifications in the url\", but instead the responsewriter contains %s", w.Body.String())
	}
}
Пример #3
0
func TestServeQuestionsByFilterWithInvalidAuthor(t *testing.T) {

	//Creates a request with an invalid Author
	r, err := http.NewRequest("GET", "api/questions/posted-by/NonExistent", nil)
	if err != nil {
		t.Error(err)
	}

	w := httptest.NewRecorder()

	ServeQuestionsByFilter(new(MockQuestionStore))(m.NewContext(), w, r)

	if w.Code != http.StatusBadRequest {
		t.Errorf("Expected a status code of 400, because the MockQuestionStore's FindQuestionsByAuthor method always returns nil as a result, but recieved an http status code of %d", w.Code)
	} else if w.Body.String() != "No question(s) found with the provided query\n" {
		t.Errorf("Expected the content of the responsewriter to be \"No question(s) found with the provided query\", but instead the responsewriter contains %s", w.Body.String())
	}
}
Пример #4
0
func TestServeFindUserWithInvalidUser(t *testing.T) {

	r, err := http.NewRequest("GET", "api/username/NonExistent", nil)
	if err != nil {
		t.Error(err)
	}

	w := httptest.NewRecorder()

	ServeFindUser(&MockUserStore{FindUserErr: errors.New("No user exists with the provided information"), FindUserStatusCode: http.StatusBadRequest})(m.NewContext(), w, r)

	if w.Code != http.StatusBadRequest {
		t.Errorf("Expected a status code of 400, but recieved an http status code of %d", w.Code)
	} else if w.Body.String() != "No user exists with the provided information\n" {
		t.Errorf("Expected the content of the responsewriter to be \"No user exists with the provided information\", but instead the responsewriter contains %s", w.Body.String())
	}
}