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