mirror of
https://github.com/netbirdio/netbird.git
synced 2025-07-19 15:45:03 +02:00
Add an upload bundle option with the flag --upload-bundle; by default, the upload will use a NetBird address, which can be replaced using the flag --upload-bundle-url. The upload server is available under the /upload-server path. The release change will push a docker image to netbirdio/upload image repository. The server supports using s3 with pre-signed URL for direct upload and local file for storing bundles.
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/netbirdio/netbird/upload-server/types"
|
|
)
|
|
|
|
func Test_LocalHandlerGetUploadURL(t *testing.T) {
|
|
mockURL := "http://localhost:8080"
|
|
t.Setenv("SERVER_URL", mockURL)
|
|
t.Setenv("STORE_DIR", t.TempDir())
|
|
|
|
mux := http.NewServeMux()
|
|
err := configureLocalHandlers(mux)
|
|
require.NoError(t, err)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, types.GetURLPath+"?id=test-file", nil)
|
|
req.Header.Set(types.ClientHeader, types.ClientHeaderValue)
|
|
|
|
rec := httptest.NewRecorder()
|
|
mux.ServeHTTP(rec, req)
|
|
|
|
require.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
var response types.GetURLResponse
|
|
err = json.Unmarshal(rec.Body.Bytes(), &response)
|
|
require.NoError(t, err)
|
|
require.Contains(t, response.URL, "test-file/")
|
|
require.NotEmpty(t, response.Key)
|
|
require.Contains(t, response.Key, "test-file/")
|
|
|
|
}
|
|
|
|
func Test_LocalHandlePutRequest(t *testing.T) {
|
|
mockDir := t.TempDir()
|
|
mockURL := "http://localhost:8080"
|
|
t.Setenv("SERVER_URL", mockURL)
|
|
t.Setenv("STORE_DIR", mockDir)
|
|
|
|
mux := http.NewServeMux()
|
|
err := configureLocalHandlers(mux)
|
|
require.NoError(t, err)
|
|
|
|
fileContent := []byte("test file content")
|
|
req := httptest.NewRequest(http.MethodPut, putURLPath+"/uploads/test.txt", bytes.NewReader(fileContent))
|
|
|
|
rec := httptest.NewRecorder()
|
|
mux.ServeHTTP(rec, req)
|
|
|
|
require.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
expectedFilePath := filepath.Join(mockDir, "uploads", "test.txt")
|
|
createdFileContent, err := os.ReadFile(expectedFilePath)
|
|
require.NoError(t, err)
|
|
require.Equal(t, fileContent, createdFileContent)
|
|
}
|