diff --git a/src/github.com/matrix-org/rageshake/submit.go b/src/github.com/matrix-org/rageshake/submit.go index 17bf71e..e530a8e 100644 --- a/src/github.com/matrix-org/rageshake/submit.go +++ b/src/github.com/matrix-org/rageshake/submit.go @@ -451,15 +451,16 @@ func (s *submitServer) saveReport(ctx context.Context, p parsedPayload, reportDi } func buildGithubIssueRequest(p parsedPayload, listingURL string) github.IssueRequest { + // set the title to the first (non-empty) line of the user's report, if any var title string - if p.UserText == "" { + trimmedUserText := strings.TrimSpace(p.UserText) + if trimmedUserText == "" { title = "Untitled report" } else { - // set the title to the first line of the user's report - if i := strings.IndexAny(p.UserText, "\r\n"); i < 0 { - title = p.UserText + if i := strings.IndexAny(trimmedUserText, "\r\n"); i < 0 { + title = trimmedUserText } else { - title = p.UserText[0:i] + title = trimmedUserText[0:i] } } diff --git a/src/github.com/matrix-org/rageshake/submit_test.go b/src/github.com/matrix-org/rageshake/submit_test.go index 88ca6ff..76d4372 100644 --- a/src/github.com/matrix-org/rageshake/submit_test.go +++ b/src/github.com/matrix-org/rageshake/submit_test.go @@ -361,3 +361,66 @@ func mkTempDir(t *testing.T) string { } return td } + +/***************************************************************************** + * + * buildGithubIssueRequest tests + */ + +func TestBuildGithubIssueLeadingNewline(t *testing.T) { + body := `------WebKitFormBoundarySsdgl8Nq9voFyhdO +Content-Disposition: form-data; name="text" + + +test words. +------WebKitFormBoundarySsdgl8Nq9voFyhdO +Content-Disposition: form-data; name="app" + +riot-web +------WebKitFormBoundarySsdgl8Nq9voFyhdO-- +` + p, _ := testParsePayload(t, body, + "multipart/form-data; boundary=----WebKitFormBoundarySsdgl8Nq9voFyhdO", + "", + ) + + if p == nil { + t.Fatal("parseRequest returned nil") + } + + issueReq := buildGithubIssueRequest(*p, "http://test/listing/foo") + + if *issueReq.Title != "test words." { + t.Errorf("Title: got %s, want %s", *issueReq.Title, "test words.") + } + expectedBody := "User message:\n```\n\ntest words.\n```" + if !strings.HasPrefix(*issueReq.Body, expectedBody) { + t.Errorf("Body: got %s, want %s", *issueReq.Body, expectedBody) + } +} + +func TestBuildGithubIssueEmptyBody(t *testing.T) { + body := `------WebKitFormBoundarySsdgl8Nq9voFyhdO +Content-Disposition: form-data; name="text" + +------WebKitFormBoundarySsdgl8Nq9voFyhdO-- +` + p, _ := testParsePayload(t, body, + "multipart/form-data; boundary=----WebKitFormBoundarySsdgl8Nq9voFyhdO", + "", + ) + + if p == nil { + t.Fatal("parseRequest returned nil") + } + + issueReq := buildGithubIssueRequest(*p, "http://test/listing/foo") + + if *issueReq.Title != "Untitled report" { + t.Errorf("Title: got %s, want %s", *issueReq.Title, "Untitled report") + } + expectedBody := "User message:\n```\n\n```" + if !strings.HasPrefix(*issueReq.Body, expectedBody) { + t.Errorf("Body: got %s, want %s", *issueReq.Body, expectedBody) + } +}