* TestPool * Store infra * Store tests updates * Bump maximum concurrent postgres connections * More infra * channels/jobs * channels/app * channels/api4 * Protect i18n from concurrent access * Replace some use of os.Setenv * Remove debug * Lint fixes * Fix more linting * Fix test * Remove use of Setenv in drafts tests * Fix flaky TestWebHubCloseConnOnDBFail * Fix merge * [MM-62408] Add CI job to generate test coverage (#30284) * Add CI job to generate test coverage * Remove use of Setenv in drafts tests * Fix flaky TestWebHubCloseConnOnDBFail * Fix more Setenv usage * Fix more potential flakyness * Remove parallelism from flaky test * Remove conflicting env var * Fix * Disable parallelism * Test atomic covermode * Disable parallelism * Enable parallelism * Add upload coverage step * Fix codecov.yml * Add codecov.yml * Remove redundant workspace field * Add Parallel() util methods and refactor * Fix formatting * More formatting fixes * Fix reporting
76 строки
1.7 KiB
Go
76 строки
1.7 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
)
|
|
|
|
func TestDownloadFromURL(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t)
|
|
defer th.TearDown()
|
|
|
|
app := th.App
|
|
app.Config().PluginSettings.AllowInsecureDownloadURL = model.NewPointer(true)
|
|
|
|
// To keep track of how many times an endpoint is retried. This needs to be reset
|
|
// for each test run.
|
|
retries := 0
|
|
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/succeeds-after-retry", func(w http.ResponseWriter, r *http.Request) {
|
|
if retries < 2 {
|
|
http.Error(w, "Request Timed out", http.StatusGatewayTimeout)
|
|
retries++
|
|
return
|
|
}
|
|
|
|
_, _ = w.Write([]byte("Your request is successful."))
|
|
})
|
|
|
|
mux.HandleFunc("/fails-forever", func(w http.ResponseWriter, r *http.Request) {
|
|
http.Error(w, "This would fail forever", http.StatusInternalServerError)
|
|
})
|
|
|
|
testServer := httptest.NewServer(mux)
|
|
|
|
tests := []struct {
|
|
name string
|
|
downloadURL string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Should succeed after two retries",
|
|
downloadURL: fmt.Sprintf("%s/succeeds-after-retry", testServer.URL),
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Should not retry forever",
|
|
downloadURL: fmt.Sprintf("%s/fails-forever", testServer.URL),
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
retries = 0 // reset the retires
|
|
_, err := th.App.DownloadFromURL(tt.downloadURL)
|
|
|
|
if tt.wantErr {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|