github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/lfs/transferadapter_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package lfs
     7  
     8  import (
     9  	"bytes"
    10  	"context"
    11  	"io"
    12  	"net/http"
    13  	"strings"
    14  	"testing"
    15  
    16  	"github.com/gitbundle/modules/json"
    17  
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  func TestBasicTransferAdapterName(t *testing.T) {
    22  	a := &BasicTransferAdapter{}
    23  
    24  	assert.Equal(t, "basic", a.Name())
    25  }
    26  
    27  func TestBasicTransferAdapter(t *testing.T) {
    28  	p := Pointer{Oid: "b5a2c96250612366ea272ffac6d9744aaf4b45aacd96aa7cfcb931ee3b558259", Size: 5}
    29  
    30  	roundTripHandler := func(req *http.Request) *http.Response {
    31  		assert.Equal(t, MediaType, req.Header.Get("Accept"))
    32  		assert.Equal(t, "test-value", req.Header.Get("test-header"))
    33  
    34  		url := req.URL.String()
    35  		if strings.Contains(url, "download-request") {
    36  			assert.Equal(t, "GET", req.Method)
    37  
    38  			return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewBufferString("dummy"))}
    39  		} else if strings.Contains(url, "upload-request") {
    40  			assert.Equal(t, "PUT", req.Method)
    41  			assert.Equal(t, "application/octet-stream", req.Header.Get("Content-Type"))
    42  
    43  			b, err := io.ReadAll(req.Body)
    44  			assert.NoError(t, err)
    45  			assert.Equal(t, "dummy", string(b))
    46  
    47  			return &http.Response{StatusCode: http.StatusOK}
    48  		} else if strings.Contains(url, "verify-request") {
    49  			assert.Equal(t, "POST", req.Method)
    50  			assert.Equal(t, MediaType, req.Header.Get("Content-Type"))
    51  
    52  			var vp Pointer
    53  			err := json.NewDecoder(req.Body).Decode(&vp)
    54  			assert.NoError(t, err)
    55  			assert.Equal(t, p.Oid, vp.Oid)
    56  			assert.Equal(t, p.Size, vp.Size)
    57  
    58  			return &http.Response{StatusCode: http.StatusOK}
    59  		} else if strings.Contains(url, "error-response") {
    60  			er := &ErrorResponse{
    61  				Message: "Object not found",
    62  			}
    63  			payload := new(bytes.Buffer)
    64  			json.NewEncoder(payload).Encode(er)
    65  
    66  			return &http.Response{StatusCode: http.StatusNotFound, Body: io.NopCloser(payload)}
    67  		} else {
    68  			t.Errorf("Unknown test case: %s", url)
    69  			return nil
    70  		}
    71  	}
    72  
    73  	hc := &http.Client{Transport: RoundTripFunc(roundTripHandler)}
    74  	a := &BasicTransferAdapter{hc}
    75  
    76  	t.Run("Download", func(t *testing.T) {
    77  		cases := []struct {
    78  			link          *Link
    79  			expectederror string
    80  		}{
    81  			// case 0
    82  			{
    83  				link: &Link{
    84  					Href:   "https://download-request.io",
    85  					Header: map[string]string{"test-header": "test-value"},
    86  				},
    87  				expectederror: "",
    88  			},
    89  			// case 1
    90  			{
    91  				link: &Link{
    92  					Href:   "https://error-response.io",
    93  					Header: map[string]string{"test-header": "test-value"},
    94  				},
    95  				expectederror: "Object not found",
    96  			},
    97  		}
    98  
    99  		for n, c := range cases {
   100  			_, err := a.Download(context.Background(), c.link)
   101  			if len(c.expectederror) > 0 {
   102  				assert.True(t, strings.Contains(err.Error(), c.expectederror), "case %d: '%s' should contain '%s'", n, err.Error(), c.expectederror)
   103  			} else {
   104  				assert.NoError(t, err, "case %d", n)
   105  			}
   106  		}
   107  	})
   108  
   109  	t.Run("Upload", func(t *testing.T) {
   110  		cases := []struct {
   111  			link          *Link
   112  			expectederror string
   113  		}{
   114  			// case 0
   115  			{
   116  				link: &Link{
   117  					Href:   "https://upload-request.io",
   118  					Header: map[string]string{"test-header": "test-value"},
   119  				},
   120  				expectederror: "",
   121  			},
   122  			// case 1
   123  			{
   124  				link: &Link{
   125  					Href:   "https://error-response.io",
   126  					Header: map[string]string{"test-header": "test-value"},
   127  				},
   128  				expectederror: "Object not found",
   129  			},
   130  		}
   131  
   132  		for n, c := range cases {
   133  			err := a.Upload(context.Background(), c.link, p, bytes.NewBufferString("dummy"))
   134  			if len(c.expectederror) > 0 {
   135  				assert.True(t, strings.Contains(err.Error(), c.expectederror), "case %d: '%s' should contain '%s'", n, err.Error(), c.expectederror)
   136  			} else {
   137  				assert.NoError(t, err, "case %d", n)
   138  			}
   139  		}
   140  	})
   141  
   142  	t.Run("Verify", func(t *testing.T) {
   143  		cases := []struct {
   144  			link          *Link
   145  			expectederror string
   146  		}{
   147  			// case 0
   148  			{
   149  				link: &Link{
   150  					Href:   "https://verify-request.io",
   151  					Header: map[string]string{"test-header": "test-value"},
   152  				},
   153  				expectederror: "",
   154  			},
   155  			// case 1
   156  			{
   157  				link: &Link{
   158  					Href:   "https://error-response.io",
   159  					Header: map[string]string{"test-header": "test-value"},
   160  				},
   161  				expectederror: "Object not found",
   162  			},
   163  		}
   164  
   165  		for n, c := range cases {
   166  			err := a.Verify(context.Background(), c.link, p)
   167  			if len(c.expectederror) > 0 {
   168  				assert.True(t, strings.Contains(err.Error(), c.expectederror), "case %d: '%s' should contain '%s'", n, err.Error(), c.expectederror)
   169  			} else {
   170  				assert.NoError(t, err, "case %d", n)
   171  			}
   172  		}
   173  	})
   174  }