github.com/2lambda123/git-lfs@v2.5.2+incompatible/tq/verify_test.go (about)

     1  package tq
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"sync/atomic"
     8  	"testing"
     9  
    10  	"github.com/git-lfs/git-lfs/lfsapi"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestVerifyWithoutAction(t *testing.T) {
    16  	c, _ := lfsapi.NewClient(nil)
    17  	tr := &Transfer{
    18  		Oid:  "abc",
    19  		Size: 123,
    20  	}
    21  
    22  	assert.Nil(t, verifyUpload(c, "origin", tr))
    23  }
    24  
    25  func TestVerifySuccess(t *testing.T) {
    26  	var called uint32
    27  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    28  		if r.URL.String() != "/verify" {
    29  			w.WriteHeader(http.StatusNotFound)
    30  			return
    31  		}
    32  
    33  		atomic.AddUint32(&called, 1)
    34  
    35  		assert.Equal(t, "POST", r.Method)
    36  		assert.Equal(t, "bar", r.Header.Get("Foo"))
    37  		assert.Equal(t, "29", r.Header.Get("Content-Length"))
    38  		assert.Equal(t, "application/vnd.git-lfs+json", r.Header.Get("Content-Type"))
    39  
    40  		var tr Transfer
    41  		assert.Nil(t, json.NewDecoder(r.Body).Decode(&tr))
    42  		assert.Equal(t, "abcd1234", tr.Oid)
    43  		assert.EqualValues(t, 123, tr.Size)
    44  	}))
    45  	defer srv.Close()
    46  
    47  	c, err := lfsapi.NewClient(lfsapi.NewContext(nil, nil, map[string]string{
    48  		"lfs.transfer.maxverifies": "1",
    49  	}))
    50  	require.Nil(t, err)
    51  	tr := &Transfer{
    52  		Oid:  "abcd1234",
    53  		Size: 123,
    54  		Actions: map[string]*Action{
    55  			"verify": &Action{
    56  				Href: srv.URL + "/verify",
    57  				Header: map[string]string{
    58  					"foo": "bar",
    59  				},
    60  			},
    61  		},
    62  	}
    63  
    64  	assert.Nil(t, verifyUpload(c, "origin", tr))
    65  	assert.EqualValues(t, 1, called)
    66  }