mirror of
https://tvoygit.ru/Djam/abfapi.git
synced 2025-02-23 10:22:45 +00:00
update test
This commit is contained in:
parent
fbea5c78b4
commit
2b3a7d99df
1 changed files with 18 additions and 13 deletions
29
test/main.go
29
test/main.go
|
@ -14,16 +14,16 @@ import (
|
|||
"tvoygit.ru/Djam/abfapi"
|
||||
)
|
||||
|
||||
// MockServer Represents a test HTTP server with mocks.
|
||||
// MockServer представляет собой тестовый HTTP-сервер с моками.
|
||||
type MockServer struct {
|
||||
Server *httptest.Server
|
||||
}
|
||||
|
||||
// NewMockServer Creates a new test server with mocks.
|
||||
// NewMockServer создает новый тестовый сервер с моками.
|
||||
func NewMockServer() *MockServer {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
// Mock for /api/v1/arches.json
|
||||
// Мок для /api/v1/arches.json
|
||||
mux.HandleFunc("/api/v1/arches.json", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
|
@ -36,7 +36,7 @@ func NewMockServer() *MockServer {
|
|||
json.NewEncoder(w).Encode(response)
|
||||
})
|
||||
|
||||
// Mock for /api/v1/upload
|
||||
// Мок для /api/v1/upload
|
||||
mux.HandleFunc("/api/v1/upload", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
|
@ -47,12 +47,14 @@ func NewMockServer() *MockServer {
|
|||
return
|
||||
}
|
||||
|
||||
// Парсим multipart/form-data
|
||||
err := r.ParseMultipartForm(10 << 20) // 10 MB max memory
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Получаем файл из формы
|
||||
file, handler, err := r.FormFile("file_store[file]")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
@ -60,12 +62,14 @@ func NewMockServer() *MockServer {
|
|||
}
|
||||
defer file.Close()
|
||||
|
||||
// Читаем содержимое файла
|
||||
content, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Вычисляем SHA1 хеш файла
|
||||
hasher := sha1.New()
|
||||
hasher.Write(content)
|
||||
shaHash := fmt.Sprintf("%x", hasher.Sum(nil))
|
||||
|
@ -78,7 +82,7 @@ func NewMockServer() *MockServer {
|
|||
json.NewEncoder(w).Encode(response)
|
||||
})
|
||||
|
||||
// Mock for /api/v1/file_stores/{hash}
|
||||
// Мок для /api/v1/file_stores/{hash}
|
||||
mux.HandleFunc("/api/v1/file_stores/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
|
@ -91,13 +95,14 @@ func NewMockServer() *MockServer {
|
|||
}
|
||||
shaHash := parts[4]
|
||||
|
||||
// Пример ответа для конкретного хеша
|
||||
if shaHash == "be730b67b175ac8dc96c9006e88e4166713cb1b5" {
|
||||
response := map[string]interface{}{
|
||||
"sha1_hash": shaHash,
|
||||
"file_name": "testfile.txt",
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode([]interface{}{response}) // Исправлено с Write на Encode
|
||||
json.NewEncoder(w).Encode([]interface{}{response})
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
|
@ -109,7 +114,7 @@ func NewMockServer() *MockServer {
|
|||
}
|
||||
}
|
||||
|
||||
// Close stops the test server.
|
||||
// Close останавливает тестовый сервер.
|
||||
func (ms *MockServer) Close() {
|
||||
ms.Server.Close()
|
||||
}
|
||||
|
@ -117,31 +122,31 @@ func (ms *MockServer) Close() {
|
|||
func main() {
|
||||
logger := log.New(os.Stdout, "ABF: ", log.LstdFlags)
|
||||
|
||||
// Create a test server with mocks
|
||||
// Создаем тестовый сервер с моками
|
||||
mockServer := NewMockServer()
|
||||
defer mockServer.Close()
|
||||
|
||||
// Use the test server's URL instead of real URLs
|
||||
// Используем URL тестового сервера вместо реальных URL
|
||||
abfClient, err := abfapi.NewAbfJson(mockServer.Server.URL, mockServer.Server.URL, "username", "password", logger)
|
||||
if err != nil {
|
||||
logger.Fatalf("Failed to create ABF client: %v", err)
|
||||
}
|
||||
|
||||
// Example call to the GetArchitectures method
|
||||
// Пример вызова метода GetArchitectures
|
||||
architectures, err := abfClient.GetArchitectures()
|
||||
if err != nil {
|
||||
logger.Fatalf("Failed to get architectures: %v", err)
|
||||
}
|
||||
logger.Printf("Architectures: %+v", architectures)
|
||||
|
||||
// Example call to the UploadFile method
|
||||
// Пример вызова метода UploadFile
|
||||
shaHash, err := abfClient.UploadFile("testfile.txt", false)
|
||||
if err != nil {
|
||||
logger.Fatalf("Failed to upload file: %v", err)
|
||||
}
|
||||
logger.Printf("Uploaded file SHA1: %s", shaHash)
|
||||
|
||||
// Example call to the FetchFile method
|
||||
// Пример вызова метода FetchFile
|
||||
err = abfClient.FetchFile(shaHash, "downloaded_testfile.txt")
|
||||
if err != nil {
|
||||
logger.Fatalf("Failed to fetch file: %v", err)
|
||||
|
|
Loading…
Add table
Reference in a new issue