dataset_test.go 1019 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package types
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/jarcoal/httpmock"
  8. )
  9. func TestDownladFile(t *testing.T) {
  10. examplePath := "./example.txt"
  11. defer os.Remove(examplePath)
  12. httpmock.Activate()
  13. defer httpmock.DeactivateAndReset()
  14. //OK
  15. httpmock.RegisterResponder(
  16. "GET",
  17. "https://example.com/xx",
  18. httpmock.NewStringResponder(200, "example content oneoneone"),
  19. )
  20. httpmock.RegisterResponder(
  21. "GET",
  22. "https://example.com/x",
  23. httpmock.NewStringResponder(404, "not found"),
  24. )
  25. err := downloadFile("https://example.com/xx", examplePath)
  26. assert.NoError(t, err)
  27. content, err := ioutil.ReadFile(examplePath)
  28. assert.Equal(t, "example content oneoneone", string(content))
  29. assert.NoError(t, err)
  30. //bad uri
  31. err = downloadFile("https://zz.com", examplePath)
  32. assert.Error(t, err)
  33. //404
  34. err = downloadFile("https://example.com/x", examplePath)
  35. assert.Error(t, err)
  36. //bad target
  37. err = downloadFile("https://example.com/xx", "")
  38. assert.Error(t, err)
  39. }