go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/encryptedcookies/internal/utils_test.go (about)

     1  // Copyright 2021 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package internal
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  )
    22  
    23  func TestNormalizeURL(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	Convey("Normalizes good URLs", t, func(ctx C) {
    27  		cases := []struct {
    28  			in  string
    29  			out string
    30  		}{
    31  			{"", "/"},
    32  			{"/", "/"},
    33  			{"/?asd=def#blah", "/?asd=def#blah"},
    34  			{"/abc/def", "/abc/def"},
    35  			{"/blah//abc///def/", "/blah/abc/def/"},
    36  			{"/blah/..//./abc/", "/abc/"},
    37  			{"/abc/%2F/def", "/abc/def"},
    38  		}
    39  		for _, c := range cases {
    40  			out, err := NormalizeURL(c.in)
    41  			if err != nil {
    42  				ctx.Printf("Failed while checking %q\n", c.in)
    43  				So(err, ShouldBeNil)
    44  			}
    45  			So(out, ShouldEqual, c.out)
    46  		}
    47  	})
    48  
    49  	Convey("Rejects bad URLs", t, func(ctx C) {
    50  		cases := []string{
    51  			"//",
    52  			"///",
    53  			"://",
    54  			":",
    55  			"http://another/abc/def",
    56  			"abc/def",
    57  			"//host.example.com",
    58  		}
    59  		for _, c := range cases {
    60  			_, err := NormalizeURL(c)
    61  			if err == nil {
    62  				ctx.Printf("Didn't fail while testing %q\n", c)
    63  			}
    64  			So(err, ShouldNotBeNil)
    65  		}
    66  	})
    67  }