Fix handling of reports with no labels

because go, and because nil != [], and because my testing wasn't adequate.
This commit is contained in:
Richard van der Hoff 2017-05-04 16:21:09 +01:00
parent 63d6917dfe
commit debd4f9535
2 changed files with 39 additions and 13 deletions

View file

@ -123,7 +123,7 @@ func (s *submitServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
resp, err := s.saveReport(req.Context(), *p, reportDir, listingURL) resp, err := s.saveReport(req.Context(), *p, reportDir, listingURL)
if err != nil { if err != nil {
log.Println("Error handling report", err) log.Println("Error handling report submission:", err)
http.Error(w, "Internal error", 500) http.Error(w, "Internal error", 500)
return return
} }
@ -478,10 +478,16 @@ func buildGithubIssueRequest(p parsedPayload, listingURL string) github.IssueReq
} }
body := bodyBuf.String() body := bodyBuf.String()
labels := p.Labels
// go-github doesn't like nils
if labels == nil {
labels = []string{}
}
return github.IssueRequest{ return github.IssueRequest{
Title: &title, Title: &title,
Body: &body, Body: &body,
Labels: &p.Labels, Labels: &labels,
} }
} }

View file

@ -63,6 +63,9 @@ func TestEmptyJson(t *testing.T) {
if p == nil { if p == nil {
t.Fatal("parseRequest returned nil") t.Fatal("parseRequest returned nil")
} }
if len(p.Labels) != 0 {
t.Errorf("Labels: got %#v, want []", p.Labels)
}
} }
// check that we can unpick the json submitted by the android clients // check that we can unpick the json submitted by the android clients
@ -137,14 +140,6 @@ Content-Disposition: form-data; name="user_agent"
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
------WebKitFormBoundarySsdgl8Nq9voFyhdO ------WebKitFormBoundarySsdgl8Nq9voFyhdO
Content-Disposition: form-data; name="label"
label1
------WebKitFormBoundarySsdgl8Nq9voFyhdO
Content-Disposition: form-data; name="label"
label2
------WebKitFormBoundarySsdgl8Nq9voFyhdO
Content-Disposition: form-data; name="test-field" Content-Disposition: form-data; name="test-field"
Test data Test data
@ -195,9 +190,8 @@ func checkParsedMultipartUpload(t *testing.T, p *parsedPayload) {
if len(p.Data) != 3 { if len(p.Data) != 3 {
t.Errorf("Data length: got %d, want 3", len(p.Data)) t.Errorf("Data length: got %d, want 3", len(p.Data))
} }
wantedLabels := []string{"label1", "label2"} if len(p.Labels) != 0 {
if !stringSlicesEqual(p.Labels, wantedLabels) { t.Errorf("Labels: got %#v, want []", p.Labels)
t.Errorf("Labels: got %v, want %v", p.Labels, wantedLabels)
} }
wanted = "Test data" wanted = "Test data"
if p.Data["test-field"] != wanted { if p.Data["test-field"] != wanted {
@ -217,6 +211,32 @@ func checkParsedMultipartUpload(t *testing.T, p *parsedPayload) {
} }
} }
func TestLabels(t *testing.T) {
body := `------WebKitFormBoundarySsdgl8Nq9voFyhdO
Content-Disposition: form-data; name="label"
label1
------WebKitFormBoundarySsdgl8Nq9voFyhdO
Content-Disposition: form-data; name="label"
label2
------WebKitFormBoundarySsdgl8Nq9voFyhdO--
`
p, _ := testParsePayload(t, body,
"multipart/form-data; boundary=----WebKitFormBoundarySsdgl8Nq9voFyhdO",
"",
)
if p == nil {
t.Fatal("parseRequest returned nil")
}
wantedLabels := []string{"label1", "label2"}
if !stringSlicesEqual(p.Labels, wantedLabels) {
t.Errorf("Labels: got %v, want %v", p.Labels, wantedLabels)
}
}
func stringSlicesEqual(got, want []string) bool { func stringSlicesEqual(got, want []string) bool {
if len(got) != len(want) { if len(got) != len(want) {
return false return false