github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/charmhub/path/path_test.go (about)

     1  // Copyright 2020 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package path
     5  
     6  import (
     7  	"github.com/juju/testing"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  )
    11  
    12  type PathSuite struct {
    13  	testing.IsolationSuite
    14  }
    15  
    16  var _ = gc.Suite(&PathSuite{})
    17  
    18  func (s *PathSuite) TestJoin(c *gc.C) {
    19  	rawURL := MustParseURL(c, "http://foobar/v1/path/")
    20  
    21  	path := MakePath(rawURL)
    22  	appPath, err := path.Join("entity", "app")
    23  	c.Assert(err, jc.ErrorIsNil)
    24  
    25  	c.Assert(appPath.String(), gc.Equals, "http://foobar/v1/path/entity/app")
    26  }
    27  
    28  func (s *PathSuite) TestJoinMultipleTimes(c *gc.C) {
    29  	rawURL := MustParseURL(c, "http://foobar/v1/path/")
    30  
    31  	path := MakePath(rawURL)
    32  	entityPath, err := path.Join("entity")
    33  	c.Assert(err, jc.ErrorIsNil)
    34  
    35  	appPath, err := entityPath.Join("app")
    36  	c.Assert(err, jc.ErrorIsNil)
    37  
    38  	c.Assert(appPath.String(), gc.Equals, "http://foobar/v1/path/entity/app")
    39  }
    40  
    41  func (s *PathSuite) TestQuery(c *gc.C) {
    42  	rawURL := MustParseURL(c, "http://foobar/v1/path")
    43  
    44  	path := MakePath(rawURL)
    45  
    46  	newPath, err := path.Query("q", "foo")
    47  	c.Assert(err, jc.ErrorIsNil)
    48  	c.Assert(path.String(), gc.Equals, "http://foobar/v1/path")
    49  	c.Assert(newPath.String(), gc.Equals, "http://foobar/v1/path?q=foo")
    50  }
    51  
    52  func (s *PathSuite) TestQueryEmptyValue(c *gc.C) {
    53  	rawURL := MustParseURL(c, "http://foobar/v1/path")
    54  
    55  	path := MakePath(rawURL)
    56  
    57  	newPath, err := path.Query("q", "")
    58  	c.Assert(err, jc.ErrorIsNil)
    59  	c.Assert(path.String(), gc.Equals, newPath.String())
    60  	c.Assert(newPath.String(), gc.Equals, "http://foobar/v1/path")
    61  }
    62  
    63  func (s *PathSuite) TestJoinQuery(c *gc.C) {
    64  	rawURL := MustParseURL(c, "http://foobar/v1/path")
    65  
    66  	path := MakePath(rawURL)
    67  	entityPath, err := path.Join("entity")
    68  	c.Assert(err, jc.ErrorIsNil)
    69  
    70  	appPath, err := entityPath.Join("app")
    71  	c.Assert(err, jc.ErrorIsNil)
    72  
    73  	newPath, err := appPath.Query("q", "foo")
    74  	c.Assert(err, jc.ErrorIsNil)
    75  	c.Assert(appPath.String(), gc.Equals, "http://foobar/v1/path/entity/app")
    76  	c.Assert(newPath.String(), gc.Equals, "http://foobar/v1/path/entity/app?q=foo")
    77  }
    78  
    79  func (s *PathSuite) TestMultipleQueries(c *gc.C) {
    80  	rawURL := MustParseURL(c, "http://foobar/v1/path")
    81  
    82  	path := MakePath(rawURL)
    83  
    84  	newPath, err := path.Query("q", "foo1")
    85  	c.Assert(err, jc.ErrorIsNil)
    86  
    87  	newPath, err = newPath.Query("q", "foo2")
    88  	c.Assert(err, jc.ErrorIsNil)
    89  
    90  	newPath, err = newPath.Query("x", "bar")
    91  	c.Assert(err, jc.ErrorIsNil)
    92  
    93  	c.Assert(path.String(), gc.Equals, "http://foobar/v1/path")
    94  	c.Assert(newPath.String(), gc.Equals, "http://foobar/v1/path?q=foo1&q=foo2&x=bar")
    95  }
    96  
    97  func (s *PathSuite) TestQueries(c *gc.C) {
    98  	rawURL := MustParseURL(c, "http://foobar/v1/path")
    99  
   100  	path := MakePath(rawURL)
   101  
   102  	newPath0, err := path.Query("a", "foo1")
   103  	c.Assert(err, jc.ErrorIsNil)
   104  	c.Assert(newPath0.String(), gc.Equals, "http://foobar/v1/path?a=foo1")
   105  
   106  	newPath1, err := newPath0.Query("b", "foo2")
   107  	c.Assert(err, jc.ErrorIsNil)
   108  	c.Assert(newPath1.String(), gc.Equals, "http://foobar/v1/path?a=foo1&b=foo2")
   109  
   110  	newPath1, err = newPath1.Query("c", "foo3")
   111  	c.Assert(err, jc.ErrorIsNil)
   112  	c.Assert(newPath1.String(), gc.Equals, "http://foobar/v1/path?a=foo1&b=foo2&c=foo3")
   113  }