github.com/alanchchen/go-ethereum@v1.6.6-0.20170601190819-6171d01b1195/swarm/api/uri_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestParseURI(t *testing.T) {
    25  	type test struct {
    26  		uri             string
    27  		expectURI       *URI
    28  		expectErr       bool
    29  		expectRaw       bool
    30  		expectImmutable bool
    31  	}
    32  	tests := []test{
    33  		{
    34  			uri:       "",
    35  			expectErr: true,
    36  		},
    37  		{
    38  			uri:       "foo",
    39  			expectErr: true,
    40  		},
    41  		{
    42  			uri:       "bzz",
    43  			expectErr: true,
    44  		},
    45  		{
    46  			uri:       "bzz:",
    47  			expectURI: &URI{Scheme: "bzz"},
    48  		},
    49  		{
    50  			uri:             "bzzi:",
    51  			expectURI:       &URI{Scheme: "bzzi"},
    52  			expectImmutable: true,
    53  		},
    54  		{
    55  			uri:       "bzzr:",
    56  			expectURI: &URI{Scheme: "bzzr"},
    57  			expectRaw: true,
    58  		},
    59  		{
    60  			uri:       "bzz:/",
    61  			expectURI: &URI{Scheme: "bzz"},
    62  		},
    63  		{
    64  			uri:       "bzz:/abc123",
    65  			expectURI: &URI{Scheme: "bzz", Addr: "abc123"},
    66  		},
    67  		{
    68  			uri:       "bzz:/abc123/path/to/entry",
    69  			expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
    70  		},
    71  		{
    72  			uri:       "bzzr:/",
    73  			expectURI: &URI{Scheme: "bzzr"},
    74  			expectRaw: true,
    75  		},
    76  		{
    77  			uri:       "bzzr:/abc123",
    78  			expectURI: &URI{Scheme: "bzzr", Addr: "abc123"},
    79  			expectRaw: true,
    80  		},
    81  		{
    82  			uri:       "bzzr:/abc123/path/to/entry",
    83  			expectURI: &URI{Scheme: "bzzr", Addr: "abc123", Path: "path/to/entry"},
    84  			expectRaw: true,
    85  		},
    86  		{
    87  			uri:       "bzz://",
    88  			expectURI: &URI{Scheme: "bzz"},
    89  		},
    90  		{
    91  			uri:       "bzz://abc123",
    92  			expectURI: &URI{Scheme: "bzz", Addr: "abc123"},
    93  		},
    94  		{
    95  			uri:       "bzz://abc123/path/to/entry",
    96  			expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
    97  		},
    98  	}
    99  	for _, x := range tests {
   100  		actual, err := Parse(x.uri)
   101  		if x.expectErr {
   102  			if err == nil {
   103  				t.Fatalf("expected %s to error", x.uri)
   104  			}
   105  			continue
   106  		}
   107  		if err != nil {
   108  			t.Fatalf("error parsing %s: %s", x.uri, err)
   109  		}
   110  		if !reflect.DeepEqual(actual, x.expectURI) {
   111  			t.Fatalf("expected %s to return %#v, got %#v", x.uri, x.expectURI, actual)
   112  		}
   113  		if actual.Raw() != x.expectRaw {
   114  			t.Fatalf("expected %s raw to be %t, got %t", x.uri, x.expectRaw, actual.Raw())
   115  		}
   116  		if actual.Immutable() != x.expectImmutable {
   117  			t.Fatalf("expected %s immutable to be %t, got %t", x.uri, x.expectImmutable, actual.Immutable())
   118  		}
   119  	}
   120  }