github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/daemon/response_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-2015 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package daemon 21 22 import ( 23 "encoding/json" 24 "fmt" 25 "io/ioutil" 26 "net/http" 27 "net/http/httptest" 28 "os" 29 "path/filepath" 30 31 "gopkg.in/check.v1" 32 ) 33 34 type responseSuite struct{} 35 36 var _ = check.Suite(&responseSuite{}) 37 38 func (s *responseSuite) TestRespSetsLocationIfAccepted(c *check.C) { 39 rec := httptest.NewRecorder() 40 41 rsp := &resp{ 42 Status: 202, 43 Result: map[string]interface{}{ 44 "resource": "foo/bar", 45 }, 46 } 47 48 rsp.ServeHTTP(rec, nil) 49 hdr := rec.Header() 50 c.Check(hdr.Get("Location"), check.Equals, "foo/bar") 51 } 52 53 func (s *responseSuite) TestRespSetsLocationIfCreated(c *check.C) { 54 rec := httptest.NewRecorder() 55 56 rsp := &resp{ 57 Status: 201, 58 Result: map[string]interface{}{ 59 "resource": "foo/bar", 60 }, 61 } 62 63 rsp.ServeHTTP(rec, nil) 64 hdr := rec.Header() 65 c.Check(hdr.Get("Location"), check.Equals, "foo/bar") 66 } 67 68 func (s *responseSuite) TestRespDoesNotSetLocationIfOther(c *check.C) { 69 rec := httptest.NewRecorder() 70 71 rsp := &resp{ 72 Status: 418, // I'm a teapot 73 Result: map[string]interface{}{ 74 "resource": "foo/bar", 75 }, 76 } 77 78 rsp.ServeHTTP(rec, nil) 79 hdr := rec.Header() 80 c.Check(hdr.Get("Location"), check.Equals, "") 81 } 82 83 func (s *responseSuite) TestFileResponseSetsContentDisposition(c *check.C) { 84 const filename = "icon.png" 85 86 path := filepath.Join(c.MkDir(), filename) 87 err := ioutil.WriteFile(path, nil, os.ModePerm) 88 c.Check(err, check.IsNil) 89 90 rec := httptest.NewRecorder() 91 rsp := fileResponse(path) 92 req, err := http.NewRequest("GET", "", nil) 93 c.Check(err, check.IsNil) 94 95 rsp.ServeHTTP(rec, req) 96 97 hdr := rec.Header() 98 c.Check(hdr.Get("Content-Disposition"), check.Equals, 99 fmt.Sprintf("attachment; filename=%s", filename)) 100 } 101 102 // Due to how the protocol was defined the result must be sent, even if it is 103 // null. Older clients rely on this. 104 func (s *responseSuite) TestRespJSONWithNullResult(c *check.C) { 105 rj := &respJSON{Result: nil} 106 data, err := json.Marshal(rj) 107 c.Assert(err, check.IsNil) 108 c.Check(string(data), check.Equals, `{"type":"","status-code":0,"status":"","result":null}`) 109 } 110 111 func (responseSuite) TestErrorResponderPrintfsWithArgs(c *check.C) { 112 teapot := makeErrorResponder(418) 113 114 rec := httptest.NewRecorder() 115 rsp := teapot("system memory below %d%%.", 1) 116 req, err := http.NewRequest("GET", "", nil) 117 c.Assert(err, check.IsNil) 118 rsp.ServeHTTP(rec, req) 119 120 var v struct{ Result errorResult } 121 c.Assert(json.NewDecoder(rec.Body).Decode(&v), check.IsNil) 122 123 c.Check(v.Result.Message, check.Equals, "system memory below 1%.") 124 } 125 126 func (responseSuite) TestErrorResponderDoesNotPrintfAlways(c *check.C) { 127 teapot := makeErrorResponder(418) 128 129 rec := httptest.NewRecorder() 130 rsp := teapot("system memory below 1%.") 131 req, err := http.NewRequest("GET", "", nil) 132 c.Assert(err, check.IsNil) 133 rsp.ServeHTTP(rec, req) 134 135 var v struct{ Result errorResult } 136 c.Assert(json.NewDecoder(rec.Body).Decode(&v), check.IsNil) 137 138 c.Check(v.Result.Message, check.Equals, "system memory below 1%.") 139 }