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)
if err != nil {
log.Println("Error handling report", err)
log.Println("Error handling report submission:", err)
http.Error(w, "Internal error", 500)
return
}
@ -478,10 +478,16 @@ func buildGithubIssueRequest(p parsedPayload, listingURL string) github.IssueReq
}
body := bodyBuf.String()
labels := p.Labels
// go-github doesn't like nils
if labels == nil {
labels = []string{}
}
return github.IssueRequest{
Title: &title,
Body: &body,
Labels: &p.Labels,
Labels: &labels,
}
}

View File

@ -63,6 +63,9 @@ func TestEmptyJson(t *testing.T) {
if p == 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
@ -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
------WebKitFormBoundarySsdgl8Nq9voFyhdO
Content-Disposition: form-data; name="label"
label1
------WebKitFormBoundarySsdgl8Nq9voFyhdO
Content-Disposition: form-data; name="label"
label2
------WebKitFormBoundarySsdgl8Nq9voFyhdO
Content-Disposition: form-data; name="test-field"
Test data
@ -195,9 +190,8 @@ func checkParsedMultipartUpload(t *testing.T, p *parsedPayload) {
if len(p.Data) != 3 {
t.Errorf("Data length: got %d, want 3", len(p.Data))
}
wantedLabels := []string{"label1", "label2"}
if !stringSlicesEqual(p.Labels, wantedLabels) {
t.Errorf("Labels: got %v, want %v", p.Labels, wantedLabels)
if len(p.Labels) != 0 {
t.Errorf("Labels: got %#v, want []", p.Labels)
}
wanted = "Test data"
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 {
if len(got) != len(want) {
return false