go-hep.org/x/hep@v0.38.1/xrootd/xrdio/parse_test.go (about)

     1  // Copyright ©2020 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package xrdio
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  )
    11  
    12  func TestParse(t *testing.T) {
    13  	for _, tc := range []struct {
    14  		name string
    15  		err  error
    16  		want URL
    17  	}{
    18  		{
    19  			name: "root://example.org/file1.root",
    20  			want: URL{
    21  				Addr: "example.org",
    22  				User: "",
    23  				Path: "/file1.root",
    24  			},
    25  		},
    26  		{
    27  			name: "xroot://example.org/file1.root",
    28  			want: URL{
    29  				Addr: "example.org",
    30  				User: "",
    31  				Path: "/file1.root",
    32  			},
    33  		},
    34  		{
    35  			name: "root://example.org//file1.root",
    36  			want: URL{
    37  				Addr: "example.org",
    38  				User: "",
    39  				Path: "/file1.root",
    40  			},
    41  		},
    42  		{
    43  			name: "root://bob@example.org/file1.root",
    44  			want: URL{
    45  				Addr: "example.org",
    46  				User: "bob",
    47  				Path: "/file1.root",
    48  			},
    49  		},
    50  		{
    51  			name: "root://bob:s3cr3t@example.org/file1.root",
    52  			want: URL{
    53  				Addr: "example.org",
    54  				User: "bob",
    55  				Path: "/file1.root",
    56  			},
    57  		},
    58  		{
    59  			name: "root://bob:s3cr3t@example.org:1024/file1.root",
    60  			want: URL{
    61  				Addr: "example.org:1024",
    62  				User: "bob",
    63  				Path: "/file1.root",
    64  			},
    65  		},
    66  		{
    67  			name: "root://bob:s3cr3t@example.org:1024/dir/file1.root",
    68  			want: URL{
    69  				Addr: "example.org:1024",
    70  				User: "bob",
    71  				Path: "/dir/file1.root",
    72  			},
    73  		},
    74  		{
    75  			name: "root://example.org/file1.%c.root",
    76  			want: URL{
    77  				Addr: "example.org",
    78  				Path: "/file1.%c.root",
    79  			},
    80  		},
    81  		{
    82  			name: "root://localhost:1094/file1.root",
    83  			want: URL{
    84  				Addr: "localhost:1094",
    85  				Path: "/file1.root",
    86  			},
    87  		},
    88  		{
    89  			name: "root://127.0.0.1:1094/file1.root",
    90  			want: URL{
    91  				Addr: "127.0.0.1:1094",
    92  				Path: "/file1.root",
    93  			},
    94  		},
    95  		{
    96  			name: "root://[2001:db8:85a3:8d3:1319:8a2e:370:7348]/file1.root",
    97  			want: URL{
    98  				Addr: "[2001:db8:85a3:8d3:1319:8a2e:370:7348]",
    99  				Path: "/file1.root",
   100  			},
   101  		},
   102  		{
   103  			name: "root://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:1094/file1.root",
   104  			want: URL{
   105  				Addr: "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:1094",
   106  				Path: "/file1.root",
   107  			},
   108  		},
   109  		{
   110  			name: "root://[::1]:1094/file1.root",
   111  			want: URL{
   112  				Addr: "[::1]:1094",
   113  				Path: "/file1.root",
   114  			},
   115  		},
   116  		{
   117  			name: "root://[::1%lo0]:1094/file1.root",
   118  			want: URL{
   119  				Addr: "[::1%lo0]:1094",
   120  				Path: "/file1.root",
   121  			},
   122  		},
   123  		{
   124  			name: "file:///dir/file1.root",
   125  			want: URL{
   126  				Addr: "",
   127  				Path: "/dir/file1.root",
   128  			},
   129  		},
   130  		{
   131  			// this is an incorrectly written URI.
   132  			// unfortunately, we can't distinguish it from other well-formed URIs
   133  			name: "file://dir/file1.root",
   134  			want: URL{
   135  				Addr: "dir",
   136  				Path: "/file1.root",
   137  			},
   138  		},
   139  		{
   140  			name: "dir/file1.root",
   141  			want: URL{
   142  				Addr: "",
   143  				Path: "dir/file1.root",
   144  			},
   145  		},
   146  		{
   147  			name: "root://example.org:1:2/file1.root",
   148  			err:  fmt.Errorf(`could not parse URI "root://example.org:1:2/file1.root": could not extract host+port from URI: address example.org:1:2: too many colons in address`),
   149  		},
   150  		{
   151  			name: "root://user@example.org:1:2/file1.root",
   152  			err:  fmt.Errorf(`could not parse URI "root://user@example.org:1:2/file1.root": could not extract host+port from URI: address example.org:1:2: too many colons in address`),
   153  		},
   154  		{
   155  			name: "root://user:pass@example.org:1:2/file1.root",
   156  			err:  fmt.Errorf(`could not parse URI "root://user:pass@example.org:1:2/file1.root": could not extract host+port from URI: address example.org:1:2: too many colons in address`),
   157  		},
   158  		{
   159  			name: "root://user:pass@[::1]:1:2/file1.root",
   160  			err:  fmt.Errorf(`could not parse URI "root://user:pass@[::1]:1:2/file1.root": could not extract host+port from URI: address [::1]:1:2: too many colons in address`),
   161  		},
   162  	} {
   163  		t.Run(tc.name, func(t *testing.T) {
   164  			got, err := Parse(tc.name)
   165  			switch {
   166  			case err != nil && tc.err != nil:
   167  				if got, want := err.Error(), tc.err.Error(); got != want {
   168  					t.Fatalf("invalid error:\ngot= %v\nwant=%v",
   169  						got, want,
   170  					)
   171  				}
   172  				return
   173  			case err != nil && tc.err == nil:
   174  				t.Fatalf("could not parse URI: %+v", err)
   175  			case err == nil && tc.err != nil:
   176  				t.Fatalf("expected an error: %+v", tc.err)
   177  			case err == nil && tc.err == nil:
   178  				// ok.
   179  			}
   180  
   181  			if got, want := got, tc.want; got != want {
   182  				t.Fatalf("invalid parse result:\ngot= %#v\nwant=%#v",
   183  					got, want,
   184  				)
   185  			}
   186  		})
   187  	}
   188  }