You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

transferadapter_test.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package lfs
  5. import (
  6. "bytes"
  7. "context"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. "testing"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestBasicTransferAdapterName(t *testing.T) {
  15. a := &BasicTransferAdapter{}
  16. assert.Equal(t, "basic", a.Name())
  17. }
  18. func TestBasicTransferAdapterDownload(t *testing.T) {
  19. roundTripHandler := func(req *http.Request) *http.Response {
  20. url := req.URL.String()
  21. if strings.Contains(url, "valid-download-request") {
  22. assert.Equal(t, "GET", req.Method)
  23. assert.Equal(t, "test-value", req.Header.Get("test-header"))
  24. return &http.Response{StatusCode: http.StatusOK, Body: ioutil.NopCloser(bytes.NewBufferString("dummy"))}
  25. }
  26. t.Errorf("Unknown test case: %s", url)
  27. return nil
  28. }
  29. hc := &http.Client{Transport: RoundTripFunc(roundTripHandler)}
  30. a := &BasicTransferAdapter{hc}
  31. var cases = []struct {
  32. response *ObjectResponse
  33. expectederror string
  34. }{
  35. // case 0
  36. {
  37. response: &ObjectResponse{},
  38. expectederror: "Action 'download' not found",
  39. },
  40. // case 1
  41. {
  42. response: &ObjectResponse{
  43. Actions: map[string]*Link{"upload": nil},
  44. },
  45. expectederror: "Action 'download' not found",
  46. },
  47. // case 2
  48. {
  49. response: &ObjectResponse{
  50. Actions: map[string]*Link{"download": {
  51. Href: "https://valid-download-request.io",
  52. Header: map[string]string{"test-header": "test-value"},
  53. }},
  54. },
  55. expectederror: "",
  56. },
  57. }
  58. for n, c := range cases {
  59. _, err := a.Download(context.Background(), c.response)
  60. if len(c.expectederror) > 0 {
  61. assert.True(t, strings.Contains(err.Error(), c.expectederror), "case %d: '%s' should contain '%s'", n, err.Error(), c.expectederror)
  62. } else {
  63. assert.NoError(t, err, "case %d", n)
  64. }
  65. }
  66. }