Skip to content
Myr

closes #514, code from bobbo@b4f0b50

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Javier Provecho · committed by Javier Provecho · parent 72ffff6 · Browse files · 4 files+46 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+46 added−0 removed

  • other:+46 added−0 removed

4 files+46 added−0 removed

modified:render/render.go+1 added
18 unmodified lines
19 _ Render = Data{}20 _ Render = HTML{}21 _ HTMLRender = HTMLDebug{}22 _ HTMLRender = HTMLProduction{}23 _ Render = YAML{}24)2526func writeContentType(w http.ResponseWriter, value []string) {27 header := w.Header()
4 unmodified lines
added:render/yaml.go+29 added
1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package render67import (8 "net/http"910 "gopkg.in/yaml.v2"11)1213type YAML struct {14 Data interface{}15}1617var yamlContentType = []string{"application/x-yaml; charset=utf-8"}1819func (r YAML) Render(w http.ResponseWriter) error {20 writeContentType(w, yamlContentType)2122 bytes, err := yaml.Marshal(r.Data)23 if err != nil {24 return err25 }2627 w.Write(bytes)28 return nil29}
modified:context_test.go+11 added
431 unmodified lines
432 assert.Contains(t, w.Body.String(), "func New() *Engine {")433 assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8")434}435436// TestContextRenderYAML tests that the response is serialized as YAML437// and Content-Type is set to application/x-yaml438func TestContextRenderYAML(t *testing.T) {439 c, w, _ := CreateTestContext()440 c.YAML(201, H{"foo": "bar"})441442 assert.Equal(t, w.Code, 201)443 assert.Equal(t, w.Body.String(), "foo: bar\n")444 assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/x-yaml; charset=utf-8")445}446447func TestContextHeaders(t *testing.T) {448 c, _, _ := CreateTestContext()449 c.Header("Content-Type", "text/plain")450 c.Header("X-Custom", "value")
258 unmodified lines
modified:context.go+5 added
424 unmodified lines
425func (c *Context) XML(code int, obj interface{}) {426 c.Render(code, render.XML{Data: obj})427}428429// YAML serializes the given struct as YAML into the response body.430func (c *Context) YAML(code int, obj interface{}) {431 c.Render(code, render.YAML{Data: obj})432}433434// String writes the given string into the response body.435func (c *Context) String(code int, format string, values ...interface{}) {436 c.Status(code)437 render.WriteString(c.Writer, format, values)
130 unmodified lines