-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from celenium-io/add-tests-for-cache-response
added basic file for response tests.
- Loading branch information
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// SPDX-FileCopyrightText: 2024 PK Lab AG <[email protected]> | ||
// SPDX-License-Identifier: MIT | ||
|
||
package cache | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestNewResponseRecorder(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
recorder := NewResponseRecorder(w) | ||
if recorder.ResponseWriter != w { | ||
t.Errorf("ResponseWriter not set correctly") | ||
} | ||
} | ||
|
||
func TestWrite(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
recorder := NewResponseRecorder(w) | ||
data := []byte("test data") | ||
n, err := recorder.Write(data) | ||
if err != nil || n != len(data) { | ||
t.Errorf("Error writing data") | ||
} | ||
} | ||
|
||
func TestCopyHeaders(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
recorder := NewResponseRecorder(w) | ||
w.Header().Set("Test-Header", "Test-Value") | ||
recorder.copyHeaders() | ||
if recorder.headers.Get("Test-Header") != "Test-Value" { | ||
t.Errorf("Headers not copied correctly") | ||
} | ||
} | ||
|
||
func TestWriteHeader(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
recorder := NewResponseRecorder(w) | ||
recorder.WriteHeader(http.StatusOK) | ||
if recorder.status != http.StatusOK { | ||
t.Errorf("Status code not set correctly") | ||
} | ||
} | ||
|
||
func TestResult(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
recorder := NewResponseRecorder(w) | ||
recorder.WriteHeader(http.StatusOK) | ||
const data = "test data" | ||
_, err := recorder.Write([]byte(data)) | ||
if err != nil { | ||
t.Errorf("Error on write: %v", err) | ||
} | ||
result := recorder.Result() | ||
if result.StatusCode != http.StatusOK || string(result.Body) != data { | ||
t.Errorf("Incorrect CacheEntry returned") | ||
} | ||
} | ||
|
||
func TestEncodeDecode(t *testing.T) { | ||
entry := &CacheEntry{ | ||
Header: make(http.Header), | ||
StatusCode: http.StatusOK, | ||
Body: []byte("test data"), | ||
} | ||
data, err := entry.Encode() | ||
if err != nil { | ||
t.Errorf("Error encoding CacheEntry") | ||
} | ||
newEntry := &CacheEntry{} | ||
err = newEntry.Decode(data) | ||
if err != nil || newEntry.StatusCode != http.StatusOK || string(newEntry.Body) != "test data" { | ||
t.Errorf("Error decoding CacheEntry") | ||
} | ||
} | ||
|
||
func TestReplay(t *testing.T) { | ||
entry := &CacheEntry{ | ||
Header: make(http.Header), | ||
StatusCode: http.StatusOK, | ||
Body: []byte("test data"), | ||
} | ||
w := httptest.NewRecorder() | ||
err := entry.Replay(w) | ||
if err != nil || w.Code != http.StatusOK || w.Body.String() != "test data" { | ||
t.Errorf("Error replaying CacheEntry") | ||
} | ||
} |