mirror of
https://tvoygit.ru/Djam/abfapi.git
synced 2025-02-23 10:22:45 +00:00
add mock servers
This commit is contained in:
parent
f6256e11f0
commit
4192c3fac7
2 changed files with 121 additions and 2 deletions
10
test/main.go
10
test/main.go
|
@ -8,7 +8,13 @@ import (
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
logger := log.New(os.Stdout, "ABF: ", log.LstdFlags)
|
logger := log.New(os.Stdout, "ABF: ", log.LstdFlags)
|
||||||
abfClient, err := abfapi.NewAbfJson("https://example.com/abf", "https://example.com/filestore", "username", "password", logger)
|
|
||||||
|
// Создаем тестовый сервер с моками
|
||||||
|
mockServer := abfapi.NewMockServer()
|
||||||
|
defer mockServer.Close()
|
||||||
|
|
||||||
|
// Используем URL тестового сервера вместо реальных URL
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
@ -33,4 +39,4 @@ func main() {
|
||||||
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")
|
||||||
}
|
}
|
113
test/mock_server.go
Normal file
113
test/mock_server.go
Normal file
|
@ -0,0 +1,113 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MockServer представляет собой тестовый HTTP-сервер с моками.
|
||||||
|
type MockServer struct {
|
||||||
|
Server *httptest.Server
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMockServer создает новый тестовый сервер с моками.
|
||||||
|
func NewMockServer() *MockServer {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
|
// Мок для /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)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response := map[string]interface{}{
|
||||||
|
"architectures": []string{"x86_64", "i386", "arm64"},
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(response)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Мок для /api/v1/upload
|
||||||
|
mux.HandleFunc("/api/v1/upload", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if contentType := r.Header.Get("Content-Type"); !strings.Contains(contentType, "multipart/form-data") {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
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)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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))
|
||||||
|
|
||||||
|
response := map[string]interface{}{
|
||||||
|
"sha1_hash": shaHash,
|
||||||
|
"file_name": handler.Filename,
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(response)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Мок для /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)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
parts := strings.Split(r.URL.Path, "/")
|
||||||
|
if len(parts) < 5 {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
shaHash := parts[4]
|
||||||
|
|
||||||
|
// Пример ответа для конкретного хеша
|
||||||
|
if shaHash == "expected_sha1_hash" {
|
||||||
|
response := map[string]interface{}{
|
||||||
|
"sha1_hash": shaHash,
|
||||||
|
"file_name": "testfile.txt",
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode([]interface{}{response})
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
server := httptest.NewServer(mux)
|
||||||
|
return &MockServer{
|
||||||
|
Server: server,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close останавливает тестовый сервер.
|
||||||
|
func (ms *MockServer) Close() {
|
||||||
|
ms.Server.Close()
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue