From debd4f95356061f2e1cd393b93c7d888a816af52 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 4 May 2017 16:21:09 +0100 Subject: [PATCH] Fix handling of reports with no labels because go, and because nil != [], and because my testing wasn't adequate. --- src/github.com/matrix-org/rageshake/submit.go | 10 ++++- .../matrix-org/rageshake/submit_test.go | 42 ++++++++++++++----- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/github.com/matrix-org/rageshake/submit.go b/src/github.com/matrix-org/rageshake/submit.go index 8af6ebd..54b7f66 100644 --- a/src/github.com/matrix-org/rageshake/submit.go +++ b/src/github.com/matrix-org/rageshake/submit.go @@ -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, } } diff --git a/src/github.com/matrix-org/rageshake/submit_test.go b/src/github.com/matrix-org/rageshake/submit_test.go index db4c895..b1ed8c3 100644 --- a/src/github.com/matrix-org/rageshake/submit_test.go +++ b/src/github.com/matrix-org/rageshake/submit_test.go @@ -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