github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/swarm/api/uri_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package api
    13  
    14  import (
    15  	"reflect"
    16  	"testing"
    17  )
    18  
    19  func TestParseURI(t *testing.T) {
    20  	type test struct {
    21  		uri                       string
    22  		expectURI                 *URI
    23  		expectErr                 bool
    24  		expectRaw                 bool
    25  		expectImmutable           bool
    26  		expectList                bool
    27  		expectHash                bool
    28  		expectDeprecatedRaw       bool
    29  		expectDeprecatedImmutable bool
    30  	}
    31  	tests := []test{
    32  		{
    33  			uri:       "",
    34  			expectErr: true,
    35  		},
    36  		{
    37  			uri:       "foo",
    38  			expectErr: true,
    39  		},
    40  		{
    41  			uri:       "bzz",
    42  			expectErr: true,
    43  		},
    44  		{
    45  			uri:       "bzz:",
    46  			expectURI: &URI{Scheme: "bzz"},
    47  		},
    48  		{
    49  			uri:             "bzz-immutable:",
    50  			expectURI:       &URI{Scheme: "bzz-immutable"},
    51  			expectImmutable: true,
    52  		},
    53  		{
    54  			uri:       "bzz-raw:",
    55  			expectURI: &URI{Scheme: "bzz-raw"},
    56  			expectRaw: true,
    57  		},
    58  		{
    59  			uri:       "bzz:/",
    60  			expectURI: &URI{Scheme: "bzz"},
    61  		},
    62  		{
    63  			uri:       "bzz:/abc123",
    64  			expectURI: &URI{Scheme: "bzz", Addr: "abc123"},
    65  		},
    66  		{
    67  			uri:       "bzz:/abc123/path/to/entry",
    68  			expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
    69  		},
    70  		{
    71  			uri:       "bzz-raw:/",
    72  			expectURI: &URI{Scheme: "bzz-raw"},
    73  			expectRaw: true,
    74  		},
    75  		{
    76  			uri:       "bzz-raw:/abc123",
    77  			expectURI: &URI{Scheme: "bzz-raw", Addr: "abc123"},
    78  			expectRaw: true,
    79  		},
    80  		{
    81  			uri:       "bzz-raw:/abc123/path/to/entry",
    82  			expectURI: &URI{Scheme: "bzz-raw", Addr: "abc123", Path: "path/to/entry"},
    83  			expectRaw: true,
    84  		},
    85  		{
    86  			uri:       "bzz://",
    87  			expectURI: &URI{Scheme: "bzz"},
    88  		},
    89  		{
    90  			uri:       "bzz://abc123",
    91  			expectURI: &URI{Scheme: "bzz", Addr: "abc123"},
    92  		},
    93  		{
    94  			uri:       "bzz://abc123/path/to/entry",
    95  			expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
    96  		},
    97  		{
    98  			uri:        "bzz-hash:",
    99  			expectURI:  &URI{Scheme: "bzz-hash"},
   100  			expectHash: true,
   101  		},
   102  		{
   103  			uri:        "bzz-hash:/",
   104  			expectURI:  &URI{Scheme: "bzz-hash"},
   105  			expectHash: true,
   106  		},
   107  		{
   108  			uri:        "bzz-list:",
   109  			expectURI:  &URI{Scheme: "bzz-list"},
   110  			expectList: true,
   111  		},
   112  		{
   113  			uri:        "bzz-list:/",
   114  			expectURI:  &URI{Scheme: "bzz-list"},
   115  			expectList: true,
   116  		},
   117  		{
   118  			uri:                 "bzzr:",
   119  			expectURI:           &URI{Scheme: "bzzr"},
   120  			expectDeprecatedRaw: true,
   121  		},
   122  		{
   123  			uri:                 "bzzr:/",
   124  			expectURI:           &URI{Scheme: "bzzr"},
   125  			expectDeprecatedRaw: true,
   126  		},
   127  		{
   128  			uri:                       "bzzi:",
   129  			expectURI:                 &URI{Scheme: "bzzi"},
   130  			expectDeprecatedImmutable: true,
   131  		},
   132  		{
   133  			uri:                       "bzzi:/",
   134  			expectURI:                 &URI{Scheme: "bzzi"},
   135  			expectDeprecatedImmutable: true,
   136  		},
   137  	}
   138  	for _, x := range tests {
   139  		actual, err := Parse(x.uri)
   140  		if x.expectErr {
   141  			if err == nil {
   142  				t.Fatalf("expected %s to error", x.uri)
   143  			}
   144  			continue
   145  		}
   146  		if err != nil {
   147  			t.Fatalf("error parsing %s: %s", x.uri, err)
   148  		}
   149  		if !reflect.DeepEqual(actual, x.expectURI) {
   150  			t.Fatalf("expected %s to return %#v, got %#v", x.uri, x.expectURI, actual)
   151  		}
   152  		if actual.Raw() != x.expectRaw {
   153  			t.Fatalf("expected %s raw to be %t, got %t", x.uri, x.expectRaw, actual.Raw())
   154  		}
   155  		if actual.Immutable() != x.expectImmutable {
   156  			t.Fatalf("expected %s immutable to be %t, got %t", x.uri, x.expectImmutable, actual.Immutable())
   157  		}
   158  		if actual.List() != x.expectList {
   159  			t.Fatalf("expected %s list to be %t, got %t", x.uri, x.expectList, actual.List())
   160  		}
   161  		if actual.Hash() != x.expectHash {
   162  			t.Fatalf("expected %s hash to be %t, got %t", x.uri, x.expectHash, actual.Hash())
   163  		}
   164  		if actual.DeprecatedRaw() != x.expectDeprecatedRaw {
   165  			t.Fatalf("expected %s deprecated raw to be %t, got %t", x.uri, x.expectDeprecatedRaw, actual.DeprecatedRaw())
   166  		}
   167  		if actual.DeprecatedImmutable() != x.expectDeprecatedImmutable {
   168  			t.Fatalf("expected %s deprecated immutable to be %t, got %t", x.uri, x.expectDeprecatedImmutable, actual.DeprecatedImmutable())
   169  		}
   170  	}
   171  }