Browse Source

Add more Golang testing, including coverage

master
Teran McKinney 11 months ago
parent
commit
62218c334c
  1. 9
      rantbin.go
  2. 95
      rantbin_test.go
  3. 1
      test.sh

9
rantbin.go

@ -31,8 +31,11 @@ const MaxFiles = 128
// Max length of page. This is a path we get from the client to tell us where they're leaving feedback.
const MaxPageLength = 256
const ErrorNoFormData = "No form data found."
const ErrorNoFormData = "No form data found. Set data and optionally page next time."
const ErrorMessageTooLong = "Your message was too long."
const ErrorIdenticalFeedback = "That identical feedback already exists."
const MsgFeedbackReceived = "Feedback received."
func validateData(data string) (err error) {
// Return error string if data is too long, nil if not.
@ -144,7 +147,7 @@ func feedbackHttpHandler(w http.ResponseWriter, r *http.Request) {
_, err = os.Stat(path)
if err == nil {
log.Printf("%s already exists.", path)
http.Error(w, "That identical feedback already exists.", http.StatusBadRequest)
http.Error(w, ErrorIdenticalFeedback, http.StatusBadRequest)
return
}
@ -156,7 +159,7 @@ func feedbackHttpHandler(w http.ResponseWriter, r *http.Request) {
}
log.Printf("write: %s\n", path)
fmt.Fprint(w, "Feedback received.")
fmt.Fprint(w, MsgFeedbackReceived)
return
}

95
rantbin_test.go

@ -1,33 +1,116 @@
package main
import (
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
)
func TestFeedbackHttpHandler(t *testing.T) {
req, err := http.NewRequest("POST", "/feedback", nil)
var req *http.Request
var rr *httptest.ResponseRecorder
var handler http.HandlerFunc
var expectedBody string
var expectedStatus int
var form url.Values
var err error
// No POST data.
dir, err := ioutil.TempDir("", "rantbinTestingDir")
if err = os.Chdir(dir); err != nil {
t.Fatal(err)
}
defer os.Chdir("..")
defer os.RemoveAll(dir)
req, err = http.NewRequest(http.MethodPost, "", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(feedbackHttpHandler)
rr = httptest.NewRecorder()
handler = http.HandlerFunc(feedbackHttpHandler)
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("Got %v expected %v for empty body.", rr.Code, http.StatusBadRequest)
expectedStatus = http.StatusBadRequest
if rr.Code != expectedStatus {
t.Errorf("Got %v expected %v for empty body.", rr.Code, expectedStatus)
} else {
log.Printf("Got code %v for empty body.", rr.Code)
}
expectedBody := ErrorNoFormData + "\n"
expectedBody = ErrorNoFormData + "\n"
if rr.Body.String() != expectedBody {
t.Errorf("Got %v, expected %v", rr.Body.String(), expectedBody)
} else {
log.Printf("Got body %v for empty body.", rr.Body.String())
}
req, err = http.NewRequest("POST", "", nil)
if err != nil {
t.Fatal(err)
}
// Valid request
form = url.Values{}
form.Set("page", "such a page")
form.Set("data", "much data")
req, err = http.NewRequest(http.MethodPost, "", nil)
req.Form = form
if err != nil {
t.Fatal(err)
}
rr = httptest.NewRecorder()
handler = http.HandlerFunc(feedbackHttpHandler)
handler.ServeHTTP(rr, req)
expectedStatus = http.StatusOK
if rr.Code != expectedStatus {
t.Errorf("Got %v expected %v for valid post.", rr.Code, expectedStatus)
} else {
log.Printf("Got code %v for valid post.", rr.Code)
}
expectedBody = MsgFeedbackReceived
if rr.Body.String() != expectedBody {
t.Errorf("Got %v, expected %v", rr.Body.String(), expectedBody)
} else {
log.Printf("Got body %v for valid post.", rr.Body.String())
}
// Same request again, should fail because it's already there.
form = url.Values{}
form.Set("page", "such a page")
form.Set("data", "much data")
req, err = http.NewRequest(http.MethodPost, "", nil)
req.Form = form
if err != nil {
t.Fatal(err)
}
rr = httptest.NewRecorder()
handler = http.HandlerFunc(feedbackHttpHandler)
handler.ServeHTTP(rr, req)
expectedStatus = http.StatusBadRequest
if rr.Code != expectedStatus {
t.Errorf("Got %v expected %v for identical feedback.", rr.Code, expectedStatus)
} else {
log.Printf("Got code %v for identical feedback.", rr.Code)
}
expectedBody = ErrorIdenticalFeedback + "\n"
if rr.Body.String() != expectedBody {
t.Errorf("Got %v, expected %v", rr.Body.String(), expectedBody)
} else {
log.Printf("Got body %v for identical feedback.", rr.Body.String())
}
}

1
test.sh

@ -10,6 +10,7 @@ shellcheck "$0"
go fmt
go doc -all
go test
go test -cover
go build

Loading…
Cancel
Save