Skip to content

Commit

Permalink
Merge pull request #226 from poteto-go/20250213/response-feat
Browse files Browse the repository at this point in the history
ver 1.4
  • Loading branch information
poteto0 authored Feb 13, 2025
2 parents 03b28c9 + 62b0581 commit b36bf50
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
38 changes: 38 additions & 0 deletions poteto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"net/http"
"net/http/httptest"
"strings"
"sync"

Expand Down Expand Up @@ -42,6 +43,20 @@ type Poteto interface {
OPTIONS(path string, handler HandlerFunc) error
TRACE(path string, handler HandlerFunc) error
CONNECT(path string, handler HandlerFunc) error

// poteto.Play make ut w/o server
// EX:
// p := poteto.New()
// p.GET("/users", func(ctx poteto.Context) error {
// return ctx.JSON(http.StatusOK, map[string]string{
// "id": "1",
// "name": "tester",
// })
// })
// res := p.Play(http.MethodGet, "/users")
// resBodyStr := res.Body.String
// // => {"id":"1","name":"tester"}
Play(method, path string, body ...string) *httptest.ResponseRecorder
}

type poteto struct {
Expand Down Expand Up @@ -369,3 +384,26 @@ func (p *poteto) TRACE(path string, handler HandlerFunc) error {
func (p *poteto) CONNECT(path string, handler HandlerFunc) error {
return p.router.CONNECT(path, handler)
}

func (p *poteto) Play(method, path string, body ...string) *httptest.ResponseRecorder {
if len(body) > 2 {
panic("should be len(body) = 0 | 1")
}

resp, req := func() (*httptest.ResponseRecorder, *http.Request) {
switch len(body) {
case 1:
w := httptest.NewRecorder()
req := httptest.NewRequest(method, path, strings.NewReader(body[0]))
return w, req
default:
w := httptest.NewRecorder()
req := httptest.NewRequest(method, path, nil)
return w, req
}
}()

p.ServeHTTP(resp, req)

return resp
}
41 changes: 41 additions & 0 deletions practice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package poteto_test

import (
"net/http"
"testing"

"github.com/poteto-go/poteto"
)

func TestPotetoPlay(t *testing.T) {
p := poteto.New()

p.GET("/users", func(ctx poteto.Context) error {
return ctx.JSON(http.StatusOK, map[string]string{
"id": "1",
"name": "tester",
})
})

p.POST("/users", func(ctx poteto.Context) error {
var user map[string]string
ctx.Bind(user)
return ctx.JSON(http.StatusOK, map[string]string{
"id": "1",
"name": "tester",
})
})

res := p.Play(http.MethodGet, "/users")
respBodyStr := res.Body.String()[0 : len(res.Body.String())-1]
expected := `{"id":"1","name":"tester"}`
if respBodyStr != expected {
t.Errorf("unmatched: actual(%s) - expected(%s)", respBodyStr, expected)
}

res2 := p.Play(http.MethodPost, "/users", `{"id":"1","name":"tester"}`)
respBodyStr2 := res2.Body.String()[0 : len(res2.Body.String())-1]
if respBodyStr2 != expected {
t.Errorf("unmatched: actual(%s) - expected(%s)", respBodyStr2, expected)
}
}
10 changes: 10 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Response interface {
Header() http.Header
SetHeader(key, value string)
AddHeader(key, value string)
Unwrap() http.ResponseWriter
}

type response struct {
Expand Down Expand Up @@ -72,3 +73,12 @@ func (r *response) SetStatus(code int) {
func (r *response) Header() http.Header {
return r.Writer.Header()
}

// fullfil interface for responseController
// you can assign to responseController
// res := NewResponse(w)
// rc := http.NewResponseController(res)
// https://go.dev/src/net/http/responsecontroller.go
func (r *response) Unwrap() http.ResponseWriter {
return r.Writer
}
22 changes: 22 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"
)

func TestWriteHeader(t *testing.T) {
Expand Down Expand Up @@ -93,3 +94,24 @@ func TestSetHeader(t *testing.T) {
t.Error("Unmatched")
}
}

func TestUnwrapResponse(t *testing.T) {
w := httptest.NewRecorder()

res := NewResponse(w)

var wi http.ResponseWriter = nil
wi = res.Unwrap()
if wi == nil {
t.Error("cannot unwrap response")
}
}

func TestResponseController(t *testing.T) {
w := httptest.NewRecorder()

res := NewResponse(w)
rc := http.NewResponseController(res)
rc.SetWriteDeadline(time.Now().Add(5 * time.Second))
res.Write([]byte("done"))
}

0 comments on commit b36bf50

Please sign in to comment.