github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/client/icons_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 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 client_test 21 22 import ( 23 "errors" 24 "fmt" 25 "net/http" 26 27 "golang.org/x/xerrors" 28 . "gopkg.in/check.v1" 29 ) 30 31 const ( 32 pkgID = "chatroom.ogra" 33 ) 34 35 func (cs *clientSuite) TestClientIconCallsEndpoint(c *C) { 36 _, _ = cs.cli.Icon(pkgID) 37 c.Assert(cs.req.Method, Equals, "GET") 38 c.Assert(cs.req.URL.Path, Equals, fmt.Sprintf("/v2/icons/%s/icon", pkgID)) 39 } 40 41 func (cs *clientSuite) TestClientIconHttpError(c *C) { 42 cs.err = errors.New("fail") 43 _, err := cs.cli.Icon(pkgID) 44 c.Assert(err, ErrorMatches, ".*server: fail") 45 } 46 47 func (cs *clientSuite) TestClientIconResponseNotFound(c *C) { 48 cs.status = 404 49 _, err := cs.cli.Icon(pkgID) 50 c.Assert(err, ErrorMatches, `.*Not Found`) 51 } 52 53 func (cs *clientSuite) TestClientIconInvalidContentDisposition(c *C) { 54 cs.header = http.Header{"Content-Disposition": {"invalid"}} 55 _, err := cs.cli.Icon(pkgID) 56 c.Assert(err, ErrorMatches, `.*cannot determine filename`) 57 } 58 59 func (cs *clientSuite) TestClientIcon(c *C) { 60 cs.rsp = "pixels" 61 cs.header = http.Header{"Content-Disposition": {"attachment; filename=myicon.png"}} 62 icon, err := cs.cli.Icon(pkgID) 63 c.Assert(err, IsNil) 64 c.Assert(icon.Filename, Equals, "myicon.png") 65 c.Assert(icon.Content, DeepEquals, []byte("pixels")) 66 } 67 68 func (cs *clientSuite) TestClientIconErrIsWrapped(c *C) { 69 cs.err = errors.New("boom") 70 _, err := cs.cli.Icon("something") 71 var e xerrors.Wrapper 72 c.Assert(err, Implements, &e) 73 }