Add terms and FAQ
This commit is contained in:
parent
e45f9b3bae
commit
0bc687a538
6 changed files with 227 additions and 3 deletions
150
main_test.go
150
main_test.go
|
|
@ -260,3 +260,153 @@ func TestApiHandler(t *testing.T) {
|
|||
body, shouldContain)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTermsHandler(t *testing.T) {
|
||||
// Create a request to pass to our handler.
|
||||
req, err := http.NewRequest("GET", "/terms", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
|
||||
rr := httptest.NewRecorder()
|
||||
handler := http.HandlerFunc(TermsHandler)
|
||||
|
||||
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
|
||||
// directly and pass in our Request and ResponseRecorder.
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
// Check the status code is what we expect.
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||
status, http.StatusOK)
|
||||
}
|
||||
|
||||
// Check the response contains the expected string.
|
||||
shouldContain := "Terms of Service"
|
||||
body := rr.Body.String()
|
||||
if !assert.Contains(t, body, shouldContain) {
|
||||
t.Errorf("handler returned unexpected body: got %v want %v",
|
||||
body, shouldContain)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivacyHandler(t *testing.T) {
|
||||
// Create a request to pass to our handler.
|
||||
req, err := http.NewRequest("GET", "/privacy", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
|
||||
rr := httptest.NewRecorder()
|
||||
handler := http.HandlerFunc(PrivacyHandler)
|
||||
|
||||
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
|
||||
// directly and pass in our Request and ResponseRecorder.
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
// Check the status code is what we expect.
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||
status, http.StatusOK)
|
||||
}
|
||||
|
||||
// Check the response contains the expected string.
|
||||
shouldContain := "Privacy Policy"
|
||||
body := rr.Body.String()
|
||||
if !assert.Contains(t, body, shouldContain) {
|
||||
t.Errorf("handler returned unexpected body: got %v want %v",
|
||||
body, shouldContain)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotFoundHandler(t *testing.T) {
|
||||
// Create a request to pass to our handler.
|
||||
req, err := http.NewRequest("GET", "/notfound", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
|
||||
rr := httptest.NewRecorder()
|
||||
handler := http.HandlerFunc(NotFoundHandler)
|
||||
|
||||
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
|
||||
// directly and pass in our Request and ResponseRecorder.
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
// Check the status code is what we expect.
|
||||
if status := rr.Code; status != http.StatusNotFound {
|
||||
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||
status, http.StatusNotFound)
|
||||
}
|
||||
|
||||
// Check the response contains the expected string.
|
||||
shouldContain := "<h2>404 - Page not found</h2>"
|
||||
body := rr.Body.String()
|
||||
if !assert.Contains(t, body, shouldContain) {
|
||||
t.Errorf("handler returned unexpected body: got %v want %v",
|
||||
body, shouldContain)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMethodNotAllowedHandler(t *testing.T) {
|
||||
// Create a request to pass to our handler.
|
||||
req, err := http.NewRequest("GET", "/api", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
|
||||
rr := httptest.NewRecorder()
|
||||
handler := http.HandlerFunc(MethodNotAllowedHandler)
|
||||
|
||||
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
|
||||
// directly and pass in our Request and ResponseRecorder.
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
// Check the status code is what we expect.
|
||||
if status := rr.Code; status != http.StatusMethodNotAllowed {
|
||||
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||
status, http.StatusMethodNotAllowed)
|
||||
}
|
||||
|
||||
// Check the response contains the expected string.
|
||||
shouldContain := "<h2>405 - Method Not Allowed</h2>"
|
||||
body := rr.Body.String()
|
||||
if !assert.Contains(t, body, shouldContain) {
|
||||
t.Errorf("handler returned unexpected body: got %v want %v",
|
||||
body, shouldContain)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDonateHandler(t *testing.T) {
|
||||
// Create a request to pass to our handler.
|
||||
req, err := http.NewRequest("GET", "/donate", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
|
||||
rr := httptest.NewRecorder()
|
||||
handler := http.HandlerFunc(DonateHandler)
|
||||
|
||||
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
|
||||
// directly and pass in our Request and ResponseRecorder.
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
// Check the status code is what we expect.
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||
status, http.StatusOK)
|
||||
}
|
||||
|
||||
// Check the response contains the expected string.
|
||||
shouldContain := "tl;dr: <a href=\"https://github.com/sponsors/TheLovinator1\">GitHub Sponsors</a>"
|
||||
body := rr.Body.String()
|
||||
if !assert.Contains(t, body, shouldContain) {
|
||||
t.Errorf("handler returned unexpected body: got %v want %v",
|
||||
body, shouldContain)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue