abfapi/test/main.go
2025-01-08 00:24:00 +03:00

150 lines
5.1 KiB
Go

package main
import (
"crypto/sha1"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"tvoygit.ru/Djam/abfapi"
)
// MockServer Represents a test HTTP server with mocks.
type MockServer struct {
Server *httptest.Server
}
// NewMockServer Creates a new test server with mocks.
func NewMockServer() *MockServer {
mux := http.NewServeMux()
// Mock for /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)
})
// Mock for /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
}
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
}
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)
})
// Mock for /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 == "be730b67b175ac8dc96c9006e88e4166713cb1b5" {
response := map[string]interface{}{
"sha1_hash": shaHash,
"file_name": "testfile.txt",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Write([]interface{}{response})
} else {
w.WriteHeader(http.StatusNotFound)
}
})
server := httptest.NewServer(mux)
return &MockServer{
Server: server,
}
}
// Close stops the test server.
func (ms *MockServer) Close() {
ms.Server.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
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
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
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
err = abfClient.FetchFile(shaHash, "downloaded_testfile.txt")
if err != nil {
logger.Fatalf("Failed to fetch file: %v", err)
}
logger.Printf("File fetched successfully")
}