From b637dff352c1bb247f2ba5a1d6691cd2867393a3 Mon Sep 17 00:00:00 2001 From: Rene Zhang Date: Wed, 29 Apr 2015 22:50:32 -0400 Subject: [PATCH 1/5] Add endpoint for markdown API --- fixtures/markdown.html | 5 +++ fixtures/markdown_raw.html | 3 ++ octokit/markdown.go | 43 ++++++++++++++++++++++++ octokit/markdown_test.go | 68 ++++++++++++++++++++++++++++++++++++++ octokit/request.go | 5 +++ 5 files changed, 124 insertions(+) create mode 100644 fixtures/markdown.html create mode 100644 fixtures/markdown_raw.html create mode 100644 octokit/markdown.go create mode 100644 octokit/markdown_test.go diff --git a/fixtures/markdown.html b/fixtures/markdown.html new file mode 100644 index 0000000..02f5b9f --- /dev/null +++ b/fixtures/markdown.html @@ -0,0 +1,5 @@ +

Hello world + github/linguist#1 + cool, and + #1! +

\ No newline at end of file diff --git a/fixtures/markdown_raw.html b/fixtures/markdown_raw.html new file mode 100644 index 0000000..2cb4963 --- /dev/null +++ b/fixtures/markdown_raw.html @@ -0,0 +1,3 @@ +

Hello world github/linguist#1 + cool, and #1! +

\ No newline at end of file diff --git a/octokit/markdown.go b/octokit/markdown.go new file mode 100644 index 0000000..b218084 --- /dev/null +++ b/octokit/markdown.go @@ -0,0 +1,43 @@ +package octokit + +import ( + "io/ioutil" +) + +// MardownURL is for rendering an arbitrary markdown document +// MarkdownRawURL is for rendering raw markdown +var ( + MarkdownURL = Hyperlink("/markdown") + MarkdownRawURL = Hyperlink("/markdown/raw") +) + +// Create a MarkdownService +func (c *Client) Markdown() (m *MarkdownService) { + m = &MarkdownService{client: c} + return +} + +// A service to return HTML rendered markdown document +type MarkdownService struct { + client *Client +} + +// Renders a markdown document +func (m *MarkdownService) Render(uri *Hyperlink, requestParams interface{}) (renderedHTML string, result *Result) { + url, err := ExpandWithDefault(uri, &MarkdownURL, nil) + if err != nil { + return "", &Result{Err: err} + } + + result = sendRequest(m.client, url, func(req *Request) (*Response, error) { + req.setBody(requestParams) + return req.createResponseRaw(req.Request.Post()) + }) + + body, err := ioutil.ReadAll(result.Response.Body) + if err != nil { + return "", &Result{Err: err} + } + renderedHTML = string(body) + return +} diff --git a/octokit/markdown_test.go b/octokit/markdown_test.go new file mode 100644 index 0000000..62ad424 --- /dev/null +++ b/octokit/markdown_test.go @@ -0,0 +1,68 @@ +package octokit + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMarkdownService_JSON(t *testing.T) { + setup() + defer tearDown() + + mux.HandleFunc("/markdown", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + testBody(t, r, "{\"context\":\"github/gollum\",\"mode\":\"gfm\",\"text\":\"Hello world github/linguist#1 **cool**, and #1!\"}\n") + + respondWith(w, loadFixture("markdown.html")) + }) + + input := M{ + "text": "Hello world github/linguist#1 **cool**, and #1!", + "mode": "gfm", + "context": "github/gollum", + } + + markdown, result := client.Markdown().Render(nil, input) + assert.False(t, result.HasError()) + + expected := "

Hello world \r\n" + + " github/linguist#1\r\n" + + " cool, and \r\n" + + " #1!\r\n" + + "

" + assert.Equal(t, expected, markdown) +} + +func TestMarkdownService_RAW(t *testing.T) { + setup() + defer tearDown() + + mux.HandleFunc("/markdown/raw", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + testBody(t, r, "\"Hello world github/linguist#1 **cool**, and #1!\"\n") + + respondWith(w, loadFixture("markdown_raw.html")) + }) + + input := "Hello world github/linguist#1 **cool**, and #1!" + + markdown, result := client.Markdown().Render(&MarkdownRawURL, input) + assert.False(t, result.HasError()) + + expected := "

Hello world github/linguist#1 \r\n" + + " cool, and #1!\r\n" + + "

" + assert.Equal(t, expected, markdown) +} + +func TestMarkdownService_Failure(t *testing.T) { + setup() + defer tearDown() + + url := Hyperlink("}") + markdown, result := client.Markdown().Render(&url, nil) + assert.True(t, result.HasError()) + assert.Empty(t, markdown) +} diff --git a/octokit/request.go b/octokit/request.go index 5cd0250..8100937 100644 --- a/octokit/request.go +++ b/octokit/request.go @@ -74,6 +74,11 @@ func (r *Request) setBody(input interface{}) { r.Request.SetBody(mtype, input) } +func (r *Request) createResponseRaw(sawyerResp *sawyer.Response) (resp *Response, err error) { + resp, err = NewResponse(sawyerResp) + return +} + func (r *Request) createResponse(sawyerResp *sawyer.Response, output interface{}) (resp *Response, err error) { resp, err = NewResponse(sawyerResp) if err == nil { From bb89cec7b438fc3e2a9307d41d86b96e775c6ca7 Mon Sep 17 00:00:00 2001 From: feiranchen Date: Fri, 1 May 2015 15:22:27 -0400 Subject: [PATCH 2/5] fixed test failure due to tab format --- octokit/markdown_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/octokit/markdown_test.go b/octokit/markdown_test.go index 62ad424..fe25320 100644 --- a/octokit/markdown_test.go +++ b/octokit/markdown_test.go @@ -27,10 +27,10 @@ func TestMarkdownService_JSON(t *testing.T) { markdown, result := client.Markdown().Render(nil, input) assert.False(t, result.HasError()) - expected := "

Hello world \r\n" + - " github/linguist#1\r\n" + - " cool, and \r\n" + - " #1!\r\n" + + expected := "

Hello world \n" + + " github/linguist#1\n" + + " cool, and \n" + + " #1!\n" + "

" assert.Equal(t, expected, markdown) } @@ -51,8 +51,8 @@ func TestMarkdownService_RAW(t *testing.T) { markdown, result := client.Markdown().Render(&MarkdownRawURL, input) assert.False(t, result.HasError()) - expected := "

Hello world github/linguist#1 \r\n" + - " cool, and #1!\r\n" + + expected := "

Hello world github/linguist#1 \n" + + " cool, and #1!\n" + "

" assert.Equal(t, expected, markdown) } From f6be00e11bb1f099c0474428ee593b596f81e7c8 Mon Sep 17 00:00:00 2001 From: feiranchen Date: Sat, 2 May 2015 23:31:46 -0400 Subject: [PATCH 3/5] coded encoder and decoder processes for raw request/response --- octokit/markdown.go | 20 +++++++++++--------- octokit/markdown_test.go | 4 ++-- octokit/request.go | 21 ++++++++++++++++++--- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/octokit/markdown.go b/octokit/markdown.go index b218084..ea4303b 100644 --- a/octokit/markdown.go +++ b/octokit/markdown.go @@ -1,9 +1,5 @@ package octokit -import ( - "io/ioutil" -) - // MardownURL is for rendering an arbitrary markdown document // MarkdownRawURL is for rendering raw markdown var ( @@ -22,22 +18,28 @@ type MarkdownService struct { client *Client } -// Renders a markdown document +// Renders a markdown document with json input func (m *MarkdownService) Render(uri *Hyperlink, requestParams interface{}) (renderedHTML string, result *Result) { url, err := ExpandWithDefault(uri, &MarkdownURL, nil) if err != nil { return "", &Result{Err: err} } - result = sendRequest(m.client, url, func(req *Request) (*Response, error) { req.setBody(requestParams) - return req.createResponseRaw(req.Request.Post()) + return req.createResponseRaw(req.Request.Post(), &renderedHTML) }) + return +} - body, err := ioutil.ReadAll(result.Response.Body) +// Renders a markdown document with string input +func (m *MarkdownService) RenderRaw(uri *Hyperlink, markdownText string) (renderedHTML string, result *Result) { + url, err := ExpandWithDefault(uri, &MarkdownURL, nil) if err != nil { return "", &Result{Err: err} } - renderedHTML = string(body) + result = sendRequest(m.client, url, func(req *Request) (*Response, error) { + req.setBodyText(markdownText) + return req.createResponseRaw(req.Request.Post(), &renderedHTML) + }) return } diff --git a/octokit/markdown_test.go b/octokit/markdown_test.go index fe25320..aa73555 100644 --- a/octokit/markdown_test.go +++ b/octokit/markdown_test.go @@ -41,14 +41,14 @@ func TestMarkdownService_RAW(t *testing.T) { mux.HandleFunc("/markdown/raw", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, "\"Hello world github/linguist#1 **cool**, and #1!\"\n") + testBody(t, r, "Hello world github/linguist#1 **cool**, and #1!") respondWith(w, loadFixture("markdown_raw.html")) }) input := "Hello world github/linguist#1 **cool**, and #1!" - markdown, result := client.Markdown().Render(&MarkdownRawURL, input) + markdown, result := client.Markdown().RenderRaw(&MarkdownRawURL, input) assert.False(t, result.HasError()) expected := "

Hello world github/linguist#1 \n" + diff --git a/octokit/request.go b/octokit/request.go index 8100937..0ad02cc 100644 --- a/octokit/request.go +++ b/octokit/request.go @@ -1,8 +1,10 @@ package octokit import ( + "bytes" "github.com/jingweno/go-sawyer" "github.com/jingweno/go-sawyer/mediatype" + "io/ioutil" ) func newRequest(client *Client, urlStr string) (req *Request, err error) { @@ -74,9 +76,12 @@ func (r *Request) setBody(input interface{}) { r.Request.SetBody(mtype, input) } -func (r *Request) createResponseRaw(sawyerResp *sawyer.Response) (resp *Response, err error) { - resp, err = NewResponse(sawyerResp) - return +func (r *Request) setBodyText(input string) { + mtype, _ := mediatype.Parse(textMediaType) + buf := bytes.NewBufferString(input) + r.Header.Set("Content-Type", mtype.String()) + r.ContentLength = int64(buf.Len()) + r.Body = ioutil.NopCloser(buf) } func (r *Request) createResponse(sawyerResp *sawyer.Response, output interface{}) (resp *Response, err error) { @@ -87,3 +92,13 @@ func (r *Request) createResponse(sawyerResp *sawyer.Response, output interface{} return } + +func (r *Request) createResponseRaw(sawyerResp *sawyer.Response, output *string) (resp *Response, err error) { + resp, err = NewResponse(sawyerResp) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return resp, err + } + *output = string(body) + return +} From 2129b8cf4eb1f0e0d2ce6f7c4f09f5939e6bc40b Mon Sep 17 00:00:00 2001 From: feiranchen Date: Sun, 3 May 2015 00:24:16 -0400 Subject: [PATCH 4/5] added header 'Accept', input type string->*string --- octokit/markdown.go | 3 ++- octokit/markdown_test.go | 3 ++- octokit/request.go | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/octokit/markdown.go b/octokit/markdown.go index ea4303b..6627fdb 100644 --- a/octokit/markdown.go +++ b/octokit/markdown.go @@ -32,12 +32,13 @@ func (m *MarkdownService) Render(uri *Hyperlink, requestParams interface{}) (ren } // Renders a markdown document with string input -func (m *MarkdownService) RenderRaw(uri *Hyperlink, markdownText string) (renderedHTML string, result *Result) { +func (m *MarkdownService) RenderRaw(uri *Hyperlink, markdownText *string) (renderedHTML string, result *Result) { url, err := ExpandWithDefault(uri, &MarkdownURL, nil) if err != nil { return "", &Result{Err: err} } result = sendRequest(m.client, url, func(req *Request) (*Response, error) { + req.Header.Set("Accept", textMediaType) req.setBodyText(markdownText) return req.createResponseRaw(req.Request.Post(), &renderedHTML) }) diff --git a/octokit/markdown_test.go b/octokit/markdown_test.go index aa73555..a8047c5 100644 --- a/octokit/markdown_test.go +++ b/octokit/markdown_test.go @@ -41,6 +41,7 @@ func TestMarkdownService_RAW(t *testing.T) { mux.HandleFunc("/markdown/raw", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") + testHeader(t, r, "Accept", textMediaType) testBody(t, r, "Hello world github/linguist#1 **cool**, and #1!") respondWith(w, loadFixture("markdown_raw.html")) @@ -48,7 +49,7 @@ func TestMarkdownService_RAW(t *testing.T) { input := "Hello world github/linguist#1 **cool**, and #1!" - markdown, result := client.Markdown().RenderRaw(&MarkdownRawURL, input) + markdown, result := client.Markdown().RenderRaw(&MarkdownRawURL, &input) assert.False(t, result.HasError()) expected := "

Hello world github/linguist#1 \n" + diff --git a/octokit/request.go b/octokit/request.go index 0ad02cc..330f12d 100644 --- a/octokit/request.go +++ b/octokit/request.go @@ -76,9 +76,9 @@ func (r *Request) setBody(input interface{}) { r.Request.SetBody(mtype, input) } -func (r *Request) setBodyText(input string) { +func (r *Request) setBodyText(input *string) { mtype, _ := mediatype.Parse(textMediaType) - buf := bytes.NewBufferString(input) + buf := bytes.NewBufferString(*input) r.Header.Set("Content-Type", mtype.String()) r.ContentLength = int64(buf.Len()) r.Body = ioutil.NopCloser(buf) From ca347453a1175ed6477e0b7715a90217e29911e9 Mon Sep 17 00:00:00 2001 From: feiranchen Date: Mon, 4 May 2015 11:51:37 -0400 Subject: [PATCH 5/5] removed header setting --- octokit/markdown.go | 1 - octokit/markdown_test.go | 1 - 2 files changed, 2 deletions(-) diff --git a/octokit/markdown.go b/octokit/markdown.go index 6627fdb..0efb34a 100644 --- a/octokit/markdown.go +++ b/octokit/markdown.go @@ -38,7 +38,6 @@ func (m *MarkdownService) RenderRaw(uri *Hyperlink, markdownText *string) (rende return "", &Result{Err: err} } result = sendRequest(m.client, url, func(req *Request) (*Response, error) { - req.Header.Set("Accept", textMediaType) req.setBodyText(markdownText) return req.createResponseRaw(req.Request.Post(), &renderedHTML) }) diff --git a/octokit/markdown_test.go b/octokit/markdown_test.go index a8047c5..9b324cf 100644 --- a/octokit/markdown_test.go +++ b/octokit/markdown_test.go @@ -41,7 +41,6 @@ func TestMarkdownService_RAW(t *testing.T) { mux.HandleFunc("/markdown/raw", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testHeader(t, r, "Accept", textMediaType) testBody(t, r, "Hello world github/linguist#1 **cool**, and #1!") respondWith(w, loadFixture("markdown_raw.html"))