Skip to content

Commit

Permalink
Replace NewTemplateResponse and NewJSONResponse with JSONResp and Tem…
Browse files Browse the repository at this point in the history
…plateResp
  • Loading branch information
Mara Mihali committed Oct 14, 2020
1 parent b9de362 commit 01d472f
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 19 deletions.
12 changes: 6 additions & 6 deletions safehttp/default_dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestDefaultDispatcherValidResponse(t *testing.T) {
Parse("<h1>{{ . }}</h1>")))
var data interface{}
data = "This is an actual heading, though."
return d.Write(w, safehttp.NewTemplateResponse(t, data, nil))
return d.Write(w, safehttp.TemplateResp(t, data, nil))
},
wantBody: "<h1>This is an actual heading, though.</h1>",
},
Expand All @@ -69,7 +69,7 @@ func TestDefaultDispatcherValidResponse(t *testing.T) {
fm := map[string]interface{}{
"Token": func() string { return "Token-secret" },
}
return d.Write(w, safehttp.NewTemplateResponse(t, data, fm))
return d.Write(w, safehttp.TemplateResp(t, data, fm))
},
wantBody: `<form><input type="hidden" name="token" value="Token-secret">Content</form>`,
},
Expand All @@ -87,7 +87,7 @@ func TestDefaultDispatcherValidResponse(t *testing.T) {
fm := map[string]interface{}{
"Nonce": func() string { return "Nonce-secret" },
}
return d.Write(w, safehttp.NewTemplateResponse(t, data, fm))
return d.Write(w, safehttp.TemplateResp(t, data, fm))
},
wantBody: `<script nonce="Nonce-secret" type="application/javascript">alert("script")</script><h1>Content</h1>`,
},
Expand All @@ -98,7 +98,7 @@ func TestDefaultDispatcherValidResponse(t *testing.T) {
data := struct {
Field string `json:"field"`
}{Field: "myField"}
return d.Write(w, safehttp.NewJSONResponse(data))
return d.Write(w, safehttp.JSONResp(data))
},
wantBody: ")]}',\n{\"field\":\"myField\"}\n",
},
Expand Down Expand Up @@ -144,15 +144,15 @@ func TestDefaultDispatcherInvalidResponse(t *testing.T) {
Parse("<h1>{{ . }}</h1>")))
var data interface{}
data = "This is an actual heading, though."
return d.Write(w, safehttp.NewTemplateResponse(t, data, nil))
return d.Write(w, safehttp.TemplateResp(t, data, nil))
},
want: "",
},
{
name: "Invalid JSON Response",
write: func(w http.ResponseWriter) error {
d := &safehttp.DefaultDispatcher{}
return d.Write(w, safehttp.NewJSONResponse(math.Inf(1)))
return d.Write(w, safehttp.JSONResp(math.Inf(1)))
},
want: ")]}',\n",
},
Expand Down
8 changes: 4 additions & 4 deletions safehttp/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ type TemplateResponse struct {
// ResponseWriter.NoContent.
type NoContentResponse struct{}

// NewJSONResponse creates a JSON response from a data object. This should be a
// JSONResp creates a JSONResponse from a data object. This should be a
// valid JSON object that can be serialised and written to the
// http.ResponseWriter using a JSON encoder.
func NewJSONResponse(data interface{}) JSONResponse {
func JSONResp(data interface{}) JSONResponse {
return JSONResponse{Data: data}
}

// NewTemplateResponse creates a TemplateResponse from a Template, its data and
// TemplateResp creates a TemplateResponse from a Template, its data and
// the name to function mappings. These will be pre-processed and then written
// to the http.ResponseWriter, if deemed safe. If the funcMap is non-nil, its
// elements will override the existing name to function mappings in the
// template. An attempt to define a new name to function mapping that is not
// already in the template will result in a panic.
func NewTemplateResponse(t Template, data interface{}, funcMap map[string]interface{}) TemplateResponse {
func TemplateResp(t Template, data interface{}, funcMap map[string]interface{}) TemplateResponse {
return TemplateResponse{
Template: &t,
Data: &data,
Expand Down
4 changes: 2 additions & 2 deletions safehttp/response_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ func TestResponseWriterUnsafeResponse(t *testing.T) {
{
name: "Invalid JSON Response",
write: func(w *safehttp.ResponseWriter) {
w.Write(safehttp.NewJSONResponse(math.Inf(1)))
w.Write(safehttp.JSONResp(math.Inf(1)))
},
},
{
name: "Unsafe Template Response",
write: func(w *safehttp.ResponseWriter) {
w.Write(safehttp.NewTemplateResponse(template.
w.Write(safehttp.TemplateResp(template.
Must(template.New("name").
Parse("<h1>{{ . }}</h1>")), "This is an actual heading, though.", nil))
},
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/csp/csp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestServeMuxInstallCSP(t *testing.T) {
nonce, err = csp.Nonce(r.Context())
t := template.Must(template.New("name").Funcs(fns).Parse(`<script nonce="{{CSPNonce}}" type="application/javascript">alert("script")</script><h1>{{.}}</h1>`))

return w.Write(safehttp.NewTemplateResponse(t, "Content", fns))
return w.Write(safehttp.TemplateResp(t, "Content", fns))
})
mb.Handle("/bar", safehttp.MethodGet, handler)

Expand Down
8 changes: 4 additions & 4 deletions tests/integration/mux/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestMuxDefaultDispatcher(t *testing.T) {
{
name: "Safe HTML Template Response",
handler: safehttp.HandlerFunc(func(w *safehttp.ResponseWriter, r *safehttp.IncomingRequest) safehttp.Result {
return w.Write(safehttp.NewTemplateResponse(safetemplate.
return w.Write(safehttp.TemplateResp(safetemplate.
Must(safetemplate.New("name").
Parse("<h1>{{ . }}</h1>")), "This is an actual heading, though.", nil))
}),
Expand All @@ -63,7 +63,7 @@ func TestMuxDefaultDispatcher(t *testing.T) {
data := struct {
Field string `json:"field"`
}{Field: "myField"}
return w.Write(safehttp.NewJSONResponse(data))
return w.Write(safehttp.JSONResp(data))
}),
wantHeaders: map[string][]string{
"Content-Type": {"application/json; charset=utf-8"},
Expand Down Expand Up @@ -110,15 +110,15 @@ func TestMuxDefaultDispatcherUnsafeResponses(t *testing.T) {
{
name: "Unsafe Template Response",
handler: safehttp.HandlerFunc(func(w *safehttp.ResponseWriter, r *safehttp.IncomingRequest) safehttp.Result {
return w.Write(safehttp.NewTemplateResponse(template.
return w.Write(safehttp.TemplateResp(template.
Must(template.New("name").
Parse("<h1>{{ . }}</h1>")), "This is an actual heading, though.", nil))
}),
},
{
name: "Invalid JSON Response",
handler: safehttp.HandlerFunc(func(w *safehttp.ResponseWriter, r *safehttp.IncomingRequest) safehttp.Result {
return w.Write(safehttp.NewJSONResponse(math.Inf(1)))
return w.Write(safehttp.JSONResp(math.Inf(1)))
}),
},
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/safehtml/safehtml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestHandleRequestWrite(t *testing.T) {
func TestHandleRequestWriteTemplate(t *testing.T) {
mb := &safehttp.ServeMuxConfig{}
mb.Handle("/", safehttp.MethodGet, safehttp.HandlerFunc(func(rw *safehttp.ResponseWriter, ir *safehttp.IncomingRequest) safehttp.Result {
return rw.Write(safehttp.NewTemplateResponse(template.Must(template.New("name").Parse("<h1>{{ . }}</h1>")), "This is an actual heading, though.", nil))
return rw.Write(safehttp.TemplateResp(template.Must(template.New("name").Parse("<h1>{{ . }}</h1>")), "This is an actual heading, though.", nil))
}))

req := httptest.NewRequest(safehttp.MethodGet, "http://foo.com/", nil)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/xsrf/xsrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestServeMuxInstallXSRF(t *testing.T) {
token, err = xsrf.Token(r)
t := template.Must(template.New("name").Funcs(fns).Parse(`<form><input type="hidden" name="token" value="{{XSRFToken}}">{{.}}</form>`))

return w.Write(safehttp.NewTemplateResponse(t, "Content", fns))
return w.Write(safehttp.TemplateResp(t, "Content", fns))
})
mb.Handle("/bar", safehttp.MethodGet, handler)

Expand Down

0 comments on commit 01d472f

Please sign in to comment.