|
1 | 1 | package ditto |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | | - "context" |
6 | | - "fmt" |
7 | | - "io" |
| 4 | + "net" |
8 | 5 | "net/http" |
| 6 | + "net/http/httptest" |
9 | 7 | "os" |
10 | | - "path/filepath" |
11 | 8 | "testing" |
12 | | - |
13 | | - "github.com/google/go-github/v57/github" |
14 | 9 | ) |
15 | 10 |
|
16 | | -func TestMain(m *testing.M) { |
17 | | - os.Exit(m.Run()) |
18 | | -} |
19 | | - |
20 | | -func TestGetCacheFilePath(t *testing.T) { |
21 | | - endpoint := "https://example.com/api" |
22 | | - expected := filepath.Join(".ditto", "362656336a5f086c") |
23 | | - result := getCacheFilePath(endpoint) |
24 | | - if result != expected { |
25 | | - t.Errorf("Expected` %s, but got %s", expected, result) |
26 | | - } |
27 | | -} |
28 | | -func TestCache(t *testing.T) { |
29 | | - endpoint := "https://example.com/api" |
30 | | - data := []byte("test data") |
31 | | - expectedFilePath := getCacheFilePath(endpoint) |
32 | | - |
33 | | - err := cache(endpoint, data) |
34 | | - if err != nil { |
35 | | - t.Errorf("Unexpected error: %v", err) |
36 | | - } |
37 | | - |
38 | | - // Check if the cache file exists |
39 | | - _, err = os.Stat(expectedFilePath) |
40 | | - if err != nil { |
41 | | - t.Errorf("Cache file does not exist: %v", err) |
42 | | - } |
43 | | - |
44 | | - // Clean up the cache file |
45 | | - err = os.Remove(expectedFilePath) |
46 | | - if err != nil { |
47 | | - t.Errorf("Failed to remove cache file: %v", err) |
48 | | - } |
49 | | -} |
50 | | -func TestRetrieve(t *testing.T) { |
51 | | - endpoint := "https://example.com/api" |
52 | | - expectedData := []byte("test data") |
53 | | - expectedFilePath := getCacheFilePath(endpoint) |
54 | | - |
55 | | - // Create a cache file with test data |
56 | | - err := os.WriteFile(expectedFilePath, expectedData, 0644) |
57 | | - if err != nil { |
58 | | - t.Fatalf("Failed to create cache file: %v", err) |
59 | | - } |
60 | | - defer os.Remove(expectedFilePath) |
61 | | - |
62 | | - // Test loading cache |
63 | | - result, err := retrieve(endpoint) |
64 | | - if err != nil { |
65 | | - t.Errorf("Unexpected error: %v", err) |
66 | | - } |
| 11 | +func TestCachingTransport_RoundTrip(t *testing.T) { |
| 12 | + // Create a test server |
| 13 | + server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 14 | + w.WriteHeader(http.StatusOK) |
| 15 | + w.Write([]byte("Hello, World!")) |
| 16 | + })) |
67 | 17 |
|
68 | | - // Compare loaded data with expected data |
69 | | - if !bytes.Equal(result, expectedData) { |
70 | | - t.Errorf("Expected data: %s, but got: %s", expectedData, result) |
71 | | - } |
72 | | -} |
73 | | -func TestCachingTransport_RoundTrip_CachedResponse(t *testing.T) { |
74 | | - // Define the test URL and expected response |
75 | | - url := "https://example.com/api" |
| 18 | + // Set the server to listen on a specific address |
| 19 | + server.Listener, _ = net.Listen("tcp", "localhost:56560") |
76 | 20 |
|
77 | | - // Remove any existing cache for the test URL |
78 | | - cacheFilePath := getCacheFilePath(url) |
79 | | - _ = os.Remove(cacheFilePath) |
| 21 | + server.Start() |
| 22 | + defer server.Close() |
80 | 23 |
|
81 | | - // Create a new caching HTTP client |
82 | | - client := &CachingTransport{ |
| 24 | + // Create a new CachingTransport |
| 25 | + cachingTransport := &CachingTransport{ |
83 | 26 | Transport: http.DefaultTransport, |
84 | 27 | } |
85 | 28 |
|
86 | | - // Create a new HTTP request |
87 | | - req, err := http.NewRequest("GET", url, nil) |
| 29 | + // Create a new request |
| 30 | + req, err := http.NewRequest(http.MethodGet, server.URL, nil) |
88 | 31 | if err != nil { |
89 | | - t.Fatalf("Failed to create HTTP request: %v", err) |
| 32 | + t.Fatalf("Failed to create request: %v", err) |
90 | 33 | } |
91 | 34 |
|
92 | | - // Make the HTTP request using the caching HTTP client |
93 | | - resp, err := client.RoundTrip(req) |
94 | | - if err != nil { |
95 | | - t.Fatalf("Failed to make HTTP request: %v", err) |
96 | | - } |
97 | | - defer resp.Body.Close() |
| 35 | + // remove the cache file if it exists and defer removing it again until the end of the test |
| 36 | + cacheFilePath := getCacheFilePath(req) |
| 37 | + os.Remove(cacheFilePath) |
| 38 | + defer os.Remove(cacheFilePath) |
98 | 39 |
|
99 | | - // Read the response body |
100 | | - _, err = io.ReadAll(resp.Body) |
| 40 | + // Call the RoundTrip method |
| 41 | + resp, err := cachingTransport.RoundTrip(req) |
101 | 42 | if err != nil { |
102 | | - t.Fatalf("Failed to read response body: %v", err) |
| 43 | + t.Fatalf("RoundTrip failed: %v", err) |
103 | 44 | } |
| 45 | + defer resp.Body.Close() |
104 | 46 |
|
105 | | - // clean up the cache file again just for good measure |
106 | | - _ = os.Remove(cacheFilePath) |
107 | | - |
108 | | -} |
109 | | - |
110 | | -func TestGitHubExampleRegression(t *testing.T) { |
111 | | - os.Remove("ditto/028587485a8f03d6") |
112 | | - defer os.Remove(".ditto/028587485a8f03d6") |
113 | | - token := os.Getenv("GITHUB_TOKEN") |
114 | | - |
115 | | - // instead of http.DefaultClient we use ditto.Client() |
116 | | - client := github.NewClient(Client()).WithAuthToken(token) // auth token is optional |
117 | | - |
118 | | - // Use client... |
119 | | - repos, _, err := client.Repositories.List(context.Background(), "TimothyStiles", nil) |
120 | | - if err != nil { |
121 | | - fmt.Println("Error:", err) |
122 | | - return |
| 47 | + // Check the response |
| 48 | + if resp.StatusCode != http.StatusOK { |
| 49 | + t.Errorf("Expected status code %d, got %d", http.StatusOK, resp.StatusCode) |
123 | 50 | } |
124 | 51 |
|
125 | | - if repos[0].GetName() != "action-discord" { |
126 | | - t.Errorf("Expected %s, but got %s", "action-discord", repos[0].GetName()) |
127 | | - } |
| 52 | + // TODO: Add more assertions as needed |
128 | 53 | } |
0 commit comments