2023-06-08 14:49:51 +00:00
|
|
|
package cwhub
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
import (
|
2022-05-19 08:48:08 +00:00
|
|
|
"os"
|
2022-03-29 12:20:26 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/jarcoal/httpmock"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDownladFile(t *testing.T) {
|
2022-05-19 08:48:08 +00:00
|
|
|
examplePath := "./example.txt"
|
|
|
|
defer os.Remove(examplePath)
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
httpmock.Activate()
|
|
|
|
defer httpmock.DeactivateAndReset()
|
|
|
|
//OK
|
|
|
|
httpmock.RegisterResponder(
|
|
|
|
"GET",
|
|
|
|
"https://example.com/xx",
|
|
|
|
httpmock.NewStringResponder(200, "example content oneoneone"),
|
|
|
|
)
|
|
|
|
httpmock.RegisterResponder(
|
|
|
|
"GET",
|
|
|
|
"https://example.com/x",
|
|
|
|
httpmock.NewStringResponder(404, "not found"),
|
|
|
|
)
|
2022-05-19 08:48:08 +00:00
|
|
|
err := downloadFile("https://example.com/xx", examplePath)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.NoError(t, err)
|
2022-09-06 11:55:03 +00:00
|
|
|
content, err := os.ReadFile(examplePath)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.Equal(t, "example content oneoneone", string(content))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
//bad uri
|
2022-05-19 08:48:08 +00:00
|
|
|
err = downloadFile("https://zz.com", examplePath)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
//404
|
2022-05-19 08:48:08 +00:00
|
|
|
err = downloadFile("https://example.com/x", examplePath)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
//bad target
|
|
|
|
err = downloadFile("https://example.com/xx", "")
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|