-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathditto_test.go
67 lines (57 loc) · 1.51 KB
/
ditto_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package ditto
import (
"fmt"
"net"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestCachingTransport_RoundTrip(t *testing.T) {
// Create a test server
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello, World!"))
}))
// Set the server to listen on a specific address
server.Listener, _ = net.Listen("tcp", "localhost:56560")
server.Start()
defer server.Close()
// Create a new CachingTransport
cachingTransport := &CachingTransport{
Transport: http.DefaultTransport,
}
// Create a new request
req, err := http.NewRequest(http.MethodGet, server.URL, nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
// remove the cache file if it exists and defer removing it again until the end of the test
cacheFilePath := getCacheFilePath(req)
os.Remove(cacheFilePath)
defer os.Remove(cacheFilePath)
// Call the RoundTrip method
resp, err := cachingTransport.RoundTrip(req)
if err != nil {
t.Fatalf("RoundTrip failed: %v", err)
}
defer resp.Body.Close()
// Check the response
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, resp.StatusCode)
}
}
func TestFindGoModDir(t *testing.T) {
_, err := findGoModDir()
if err != nil {
t.Fatal(err)
}
}
func TestGetGoModuleName(t *testing.T) {
// Create a temporary directory for testing
moduleName, err := getGoModuleName()
fmt.Println(moduleName)
if err != nil {
t.Fatal(err)
}
}