github.com/avenga/couper@v1.12.2/eval/lib/url_test.go (about)

     1  package lib_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/zclconf/go-cty/cty"
     8  
     9  	"github.com/avenga/couper/config/configload"
    10  	"github.com/avenga/couper/config/request"
    11  	"github.com/avenga/couper/eval"
    12  	"github.com/avenga/couper/internal/test"
    13  )
    14  
    15  func TestURLEncode(t *testing.T) {
    16  	helper := test.New(t)
    17  
    18  	cf, err := configload.LoadBytes([]byte(`server "test" {}`), "couper.hcl")
    19  	helper.Must(err)
    20  
    21  	hclContext := cf.Context.Value(request.ContextType).(*eval.Context).HCLContext()
    22  
    23  	s := "ABC123abc\n :/?#[]@!$&'()*+,;=%"
    24  	encodedV, err := hclContext.Functions["url_encode"].Call([]cty.Value{cty.StringVal(s)})
    25  	helper.Must(err)
    26  
    27  	if !cty.String.Equals(encodedV.Type()) {
    28  		t.Errorf("Wrong return type; expected %s, got: %s", cty.String.FriendlyName(), encodedV.Type().FriendlyName())
    29  	}
    30  
    31  	encoded := encodedV.AsString()
    32  	expected := "ABC123abc%0A%20%3A%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D%25"
    33  	if encoded != expected {
    34  		t.Errorf("Wrong return value; expected %s, got: %s", expected, encoded)
    35  	}
    36  }
    37  
    38  func TestRelativeURL(t *testing.T) {
    39  	helper := test.New(t)
    40  
    41  	cf, err := configload.LoadBytes([]byte(`server "test" {}`), "couper.hcl")
    42  	helper.Must(err)
    43  
    44  	hclContext := cf.Context.Value(request.ContextType).(*eval.Context).HCLContext()
    45  	relativeURLFunc := hclContext.Functions["relative_url"]
    46  
    47  	type testCase struct {
    48  		url    string
    49  		expURL string
    50  		expErr string
    51  	}
    52  
    53  	for _, tc := range []testCase{
    54  		// Invalid
    55  		{"", "", `invalid url given: ""`},
    56  		{"rel", "", `invalid url given: "rel"`},
    57  		{"?q", "", `invalid url given: "?q"`},
    58  		{"?", "", `invalid url given: "?"`},
    59  		{"#f", "", `invalid url given: "#f"`},
    60  		{"#", "", `invalid url given: "#"`},
    61  		{"~", "", `invalid url given: "~"`},
    62  		{"abc@def.org", "", `invalid url given: "abc@def.org"`},
    63  		{"ftp://127.0.0.1", "", `invalid url given: "ftp://127.0.0.1"`},
    64  
    65  		// Valid
    66  		{"/abs", "/abs", ``},
    67  		{"//path", "/", ``},
    68  		{"///path", "/path", ``},
    69  		{"/abs:8080", "/abs:8080", ``},
    70  		{"https://abc.def:8443:9443", "/", ``},
    71  		{"http://", "/", ``},
    72  		{"http://abc", "/", ``},
    73  		{"http://abc.def", "/", ``},
    74  		{"http://abc.def?", "/?", ``},
    75  		{"http://abc.def#", "/#", ``},
    76  		{"http://abc.def/#", "/#", ``},
    77  		{"http://abc.def?#", "/?#", ``},
    78  		{"http://abc.def/?#", "/?#", ``},
    79  		{"https://abc.def/path", "/path", ``},
    80  		{"https://abc.def/path?a+b", "/path?a+b", ``},
    81  		{"https://abc.def/path?a%20b", "/path?a%20b", ``},
    82  		{"https://abc.def:8443/path?q", "/path?q", ``},
    83  		{"https://abc.def:8443/path?q#f", "/path?q#f", ``},
    84  		{"https://user:pass@abc.def:8443/path?q#f", "/path?q#f", ``},
    85  		{"//user:pass@abc.def:8443/path?q#f", "/path?q#f", ``},
    86  		{"//abc.def:8443/path?q#f", "/path?q#f", ``},
    87  	} {
    88  		t.Run(tc.url, func(subT *testing.T) {
    89  			got, err := relativeURLFunc.Call([]cty.Value{cty.StringVal(tc.url)})
    90  
    91  			if tc.expURL != "" && got.AsString() != tc.expURL {
    92  				t.Errorf("'%#v': expected %q, got %q", tc.url, tc.expURL, got.AsString())
    93  			}
    94  			if got != cty.NilVal && tc.expURL == "" {
    95  				t.Errorf("'%#v': expected 'cty.NilVal', got %q", tc.url, got.AsString())
    96  			}
    97  			if tc.expErr != "" || err != nil {
    98  				if eerr := fmt.Sprintf("%s", err); tc.expErr != eerr {
    99  					t.Errorf("'%#v': expected %q, got %q", tc.url, tc.expErr, eerr)
   100  				}
   101  			}
   102  		})
   103  	}
   104  }