github.com/ubuntu-core/snappy@v0.0.0-20210827154228-9e584df982bb/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_test 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 "github.com/snapcore/snapd/daemon" 34 ) 35 36 type responseSuite struct{} 37 38 var _ = check.Suite(&responseSuite{}) 39 40 func (s *responseSuite) TestRespSetsLocationIfAccepted(c *check.C) { 41 rec := httptest.NewRecorder() 42 43 rsp := &daemon.RespJSON{ 44 Status: 202, 45 Result: map[string]interface{}{ 46 "resource": "foo/bar", 47 }, 48 } 49 50 rsp.ServeHTTP(rec, nil) 51 hdr := rec.Header() 52 c.Check(hdr.Get("Location"), check.Equals, "foo/bar") 53 } 54 55 func (s *responseSuite) TestRespSetsLocationIfCreated(c *check.C) { 56 rec := httptest.NewRecorder() 57 58 rsp := &daemon.RespJSON{ 59 Status: 201, 60 Result: map[string]interface{}{ 61 "resource": "foo/bar", 62 }, 63 } 64 65 rsp.ServeHTTP(rec, nil) 66 hdr := rec.Header() 67 c.Check(hdr.Get("Location"), check.Equals, "foo/bar") 68 } 69 70 func (s *responseSuite) TestRespDoesNotSetLocationIfOther(c *check.C) { 71 rec := httptest.NewRecorder() 72 73 rsp := &daemon.RespJSON{ 74 Status: 418, // I'm a teapot 75 Result: map[string]interface{}{ 76 "resource": "foo/bar", 77 }, 78 } 79 80 rsp.ServeHTTP(rec, nil) 81 hdr := rec.Header() 82 c.Check(hdr.Get("Location"), check.Equals, "") 83 } 84 85 func (s *responseSuite) TestFileResponseSetsContentDisposition(c *check.C) { 86 const filename = "icon.png" 87 88 path := filepath.Join(c.MkDir(), filename) 89 err := ioutil.WriteFile(path, nil, os.ModePerm) 90 c.Check(err, check.IsNil) 91 92 rec := httptest.NewRecorder() 93 rsp := daemon.FileResponse(path) 94 req, err := http.NewRequest("GET", "", nil) 95 c.Check(err, check.IsNil) 96 97 rsp.ServeHTTP(rec, req) 98 99 hdr := rec.Header() 100 c.Check(hdr.Get("Content-Disposition"), check.Equals, 101 fmt.Sprintf("attachment; filename=%s", filename)) 102 } 103 104 // Due to how the protocol was defined the result must be sent, even if it is 105 // null. Older clients rely on this. 106 func (s *responseSuite) TestRespJSONWithNullResult(c *check.C) { 107 rj := &daemon.RespJSON{Result: nil} 108 data, err := json.Marshal(rj) 109 c.Assert(err, check.IsNil) 110 c.Check(string(data), check.Equals, `{"type":"","status-code":0,"status":"","result":null}`) 111 }