github.com/jkawamoto/roadie-azure@v0.3.5/roadie/url_test.go (about)

     1  //
     2  // roadie/url_test.go
     3  //
     4  // Copyright (c) 2017 Junpei Kawamoto
     5  //
     6  // This file is part of Roadie Azure.
     7  //
     8  // Roadie Azure is free software: you can redistribute it and/or modify
     9  // it under the terms of the GNU General Public License as published by
    10  // the Free Software Foundation, either version 3 of the License, or
    11  // (at your option) any later version.
    12  //
    13  // Roadie Azure is distributed in the hope that it will be useful,
    14  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  // GNU General Public License for more details.
    17  //
    18  // You should have received a copy of the GNU General Public License
    19  // along with Roadie Azure. If not, see <http://www.gnu.org/licenses/>.
    20  //
    21  
    22  package roadie
    23  
    24  import (
    25  	"context"
    26  	"net/url"
    27  	"testing"
    28  )
    29  
    30  func TestOpenURL(t *testing.T) {
    31  
    32  	cases := []struct {
    33  		url  string
    34  		dest string
    35  		name string
    36  	}{
    37  		// Dropbox URL without destinations.
    38  		{"dropbox://sh/hlt9248hw1u54d6/AADLBa5TfbZKAacDzoARfFhqa", "", "testing.zip"},
    39  		// Dropbox URL with a destination.
    40  		{"dropbox://sh/hlt9248hw1u54d6/AADLBa5TfbZKAacDzoARfFhqa:/tmp/", "/tmp", "testing.zip"},
    41  		// Dropbox URL with renaming.
    42  		{"dropbox://sh/hlt9248hw1u54d6/AADLBa5TfbZKAacDzoARfFhqa:/tmp/some.zip", "/tmp", "some.zip"},
    43  		// HTTP URL without destinations.
    44  		{"https://raw.githubusercontent.com/jkawamoto/roadie-azure/master/README.md", "", "README.md"},
    45  		// HTTP URL with a destination.
    46  		{"https://raw.githubusercontent.com/jkawamoto/roadie-azure/master/README.md:/tmp/", "/tmp", "README.md"},
    47  		// HTTP URL with renaming.
    48  		{"https://raw.githubusercontent.com/jkawamoto/roadie-azure/master/README.md:/tmp/README2.md", "/tmp", "README2.md"},
    49  	}
    50  
    51  	for _, c := range cases {
    52  
    53  		t.Run(c.url, func(t *testing.T) {
    54  
    55  			obj, err := OpenURL(context.Background(), c.url)
    56  			if err != nil {
    57  				t.Fatalf("OpenURL returns an error: %v", err)
    58  			}
    59  			defer obj.Body.Close()
    60  
    61  			if obj.Dest != c.dest {
    62  				t.Errorf("destination is %q, want %q", obj.Dest, c.dest)
    63  			}
    64  			if obj.Name != c.name {
    65  				t.Errorf("name %q, want %q", obj.Name, c.name)
    66  			}
    67  
    68  		})
    69  
    70  	}
    71  
    72  }
    73  
    74  func TestExpandDropboxURL(t *testing.T) {
    75  
    76  	input := "dropbox://sh/hlt9248hw1u54d6/AADLBa5TfbZKAacDzoARfFhqa"
    77  	expect := "https://www.dropbox.com/sh/hlt9248hw1u54d6/AADLBa5TfbZKAacDzoARfFhqa?dl=1"
    78  
    79  	loc, err := url.Parse(input)
    80  	if err != nil {
    81  		t.Fatalf("cannot parse a URL: %v", err)
    82  	}
    83  	res := expandDropboxURL(loc)
    84  
    85  	expectURL, err := url.Parse(expect)
    86  	if err != nil {
    87  		t.Fatalf("cannot parse a URL: %v", err)
    88  	}
    89  	if res.String() != expectURL.String() {
    90  		t.Errorf("expaned URL is %v, want %v", res, expectURL)
    91  	}
    92  
    93  }