mirror of
https://tvoygit.ru/Djam/abfapi.git
synced 2025-02-23 18:32:46 +00:00
update test
This commit is contained in:
parent
aea93aa545
commit
b3fccab775
1 changed files with 118 additions and 123 deletions
241
test/main.go
241
test/main.go
|
@ -1,155 +1,150 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"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)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
response := map[string]interface{}{
|
response := map[string]interface{}{
|
||||||
"architectures": []string{"x86_64", "i386", "arm64"},
|
"architectures": []string{"x86_64", "i386", "arm64"},
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
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)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if contentType := r.Header.Get("Content-Type"); !strings.Contains(contentType, "multipart/form-data") {
|
if contentType := r.Header.Get("Content-Type"); !strings.Contains(contentType, "multipart/form-data") {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
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)
|
return
|
||||||
return
|
}
|
||||||
}
|
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))
|
|
||||||
|
|
||||||
response := map[string]interface{}{
|
response := map[string]interface{}{
|
||||||
"sha1_hash": shaHash,
|
"sha1_hash": shaHash,
|
||||||
"file_name": handler.Filename,
|
"file_name": handler.Filename,
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
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)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
parts := strings.Split(r.URL.Path, "/")
|
parts := strings.Split(r.URL.Path, "/")
|
||||||
if len(parts) < 5 {
|
if len(parts) < 5 {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
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).Write([]interface{}{response})
|
||||||
json.NewEncoder(w).Encode([]interface{}{response})
|
} else {
|
||||||
} else {
|
w.WriteHeader(http.StatusNotFound)
|
||||||
w.WriteHeader(http.StatusNotFound)
|
}
|
||||||
}
|
})
|
||||||
})
|
|
||||||
|
|
||||||
server := httptest.NewServer(mux)
|
server := httptest.NewServer(mux)
|
||||||
return &MockServer{
|
return &MockServer{
|
||||||
Server: server,
|
Server: server,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close останавливает тестовый сервер.
|
// Close stops the test server.
|
||||||
func (ms *MockServer) Close() {
|
func (ms *MockServer) Close() {
|
||||||
ms.Server.Close()
|
ms.Server.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)
|
||||||
}
|
}
|
||||||
logger.Printf("File fetched successfully")
|
logger.Printf("File fetched successfully")
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue