github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmn/tests/url_test.go (about)

     1  // Package test provides tests for common low-level types and utilities for all aistore projects
     2  /*
     3   * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package tests_test
     6  
     7  import (
     8  	"fmt"
     9  	"net/http"
    10  	"net/url"
    11  	"testing"
    12  
    13  	"github.com/NVIDIA/aistore/api/apc"
    14  	"github.com/NVIDIA/aistore/cmn"
    15  	"github.com/NVIDIA/aistore/cmn/cos"
    16  	"github.com/NVIDIA/aistore/tools/tassert"
    17  )
    18  
    19  func TestParseURLScheme(t *testing.T) {
    20  	testCases := []struct{ expectedScheme, expectedAddress, url string }{
    21  		{"http", "localhost:8080", "http://localhost:8080"},
    22  		{"https", "localhost", "https://localhost"},
    23  		{"", "localhost:8080", "localhost:8080"},
    24  	}
    25  
    26  	for _, tc := range testCases {
    27  		scheme, address := cmn.ParseURLScheme(tc.url)
    28  		tassert.Errorf(t, scheme == tc.expectedScheme, "expected scheme %s, got %s", tc.expectedScheme, scheme)
    29  		tassert.Errorf(t, address == tc.expectedAddress, "expected address %s, got %s", tc.expectedAddress, address)
    30  	}
    31  }
    32  
    33  func TestReparseQuery(t *testing.T) {
    34  	const (
    35  		versionID = "1"
    36  		uuid      = "R9oLVoEsxx"
    37  		basePath  = "/s3/imagenet-tar/oisubset-train-0000.tar"
    38  	)
    39  
    40  	r := &http.Request{
    41  		Method: http.MethodGet,
    42  		URL: &url.URL{
    43  			Path: fmt.Sprintf("%s?%s=%s", basePath, apc.QparamUUID, uuid),
    44  		},
    45  	}
    46  	q := url.Values{}
    47  	q.Add("versionID", versionID)
    48  	r.URL.RawQuery = q.Encode()
    49  
    50  	cos.ReparseQuery(r)
    51  	actualVersionID, actualUUID := r.URL.Query().Get("versionID"), r.URL.Query().Get(apc.QparamUUID)
    52  	tassert.Errorf(t, actualVersionID == versionID, "expected versionID to be %q, got %q", versionID, actualVersionID)
    53  	tassert.Errorf(t, actualUUID == uuid, "expected %s to be %q, got %q", apc.QparamUUID, uuid, actualUUID)
    54  	tassert.Errorf(t, r.URL.Path == basePath, "expected path to be %q, got %q", basePath, r.URL.Path)
    55  }