github.com/ethanhsieh/snapd@v0.0.0-20210615102523-3db9b8e4edc5/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  
    29  	. "gopkg.in/check.v1"
    30  )
    31  
    32  const (
    33  	pkgID = "chatroom.ogra"
    34  )
    35  
    36  func (cs *clientSuite) TestClientIconCallsEndpoint(c *C) {
    37  	_, _ = cs.cli.Icon(pkgID)
    38  	c.Assert(cs.req.Method, Equals, "GET")
    39  	c.Assert(cs.req.URL.Path, Equals, fmt.Sprintf("/v2/icons/%s/icon", pkgID))
    40  }
    41  
    42  func (cs *clientSuite) TestClientIconHttpError(c *C) {
    43  	cs.err = errors.New("fail")
    44  	_, err := cs.cli.Icon(pkgID)
    45  	c.Assert(err, ErrorMatches, ".*server: fail")
    46  }
    47  
    48  func (cs *clientSuite) TestClientIconResponseNotFound(c *C) {
    49  	cs.status = 404
    50  	_, err := cs.cli.Icon(pkgID)
    51  	c.Assert(err, ErrorMatches, `.*Not Found`)
    52  }
    53  
    54  func (cs *clientSuite) TestClientIconInvalidContentDisposition(c *C) {
    55  	cs.header = http.Header{"Content-Disposition": {"invalid"}}
    56  	_, err := cs.cli.Icon(pkgID)
    57  	c.Assert(err, ErrorMatches, `.*cannot determine filename`)
    58  }
    59  
    60  func (cs *clientSuite) TestClientIcon(c *C) {
    61  	cs.rsp = "pixels"
    62  	cs.header = http.Header{"Content-Disposition": {"attachment; filename=myicon.png"}}
    63  	icon, err := cs.cli.Icon(pkgID)
    64  	c.Assert(err, IsNil)
    65  	c.Assert(icon.Filename, Equals, "myicon.png")
    66  	c.Assert(icon.Content, DeepEquals, []byte("pixels"))
    67  }
    68  
    69  func (cs *clientSuite) TestClientIconErrIsWrapped(c *C) {
    70  	cs.err = errors.New("boom")
    71  	_, err := cs.cli.Icon("something")
    72  	var e xerrors.Wrapper
    73  	c.Assert(err, Implements, &e)
    74  }