forked from cedrus-and-thuja/thyrsus-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for request header validation
- Loading branch information
1 parent
9affb71
commit 9fa8012
Showing
10 changed files
with
402 additions
and
8 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,5 @@ | ||
{ | ||
"recommendations": [ | ||
"golang.go" | ||
] | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
|
||
test: | ||
go test ./pkg/thyrsus | ||
@bash ./scripts/tests.sh | ||
|
||
fmt: | ||
go fmt $$(go list ./...) |
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
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 @@ | ||
97 |
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,238 @@ | ||
|
||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | ||
<title>thyrsus: Go Coverage Report</title> | ||
<style> | ||
body { | ||
background: black; | ||
color: rgb(80, 80, 80); | ||
} | ||
body, pre, #legend span { | ||
font-family: Menlo, monospace; | ||
font-weight: bold; | ||
} | ||
#topbar { | ||
background: black; | ||
position: fixed; | ||
top: 0; left: 0; right: 0; | ||
height: 42px; | ||
border-bottom: 1px solid rgb(80, 80, 80); | ||
} | ||
#content { | ||
margin-top: 50px; | ||
} | ||
#nav, #legend { | ||
float: left; | ||
margin-left: 10px; | ||
} | ||
#legend { | ||
margin-top: 12px; | ||
} | ||
#nav { | ||
margin-top: 10px; | ||
} | ||
#legend span { | ||
margin: 0 5px; | ||
} | ||
.cov0 { color: rgb(192, 0, 0) } | ||
.cov1 { color: rgb(128, 128, 128) } | ||
.cov2 { color: rgb(116, 140, 131) } | ||
.cov3 { color: rgb(104, 152, 134) } | ||
.cov4 { color: rgb(92, 164, 137) } | ||
.cov5 { color: rgb(80, 176, 140) } | ||
.cov6 { color: rgb(68, 188, 143) } | ||
.cov7 { color: rgb(56, 200, 146) } | ||
.cov8 { color: rgb(44, 212, 149) } | ||
.cov9 { color: rgb(32, 224, 152) } | ||
.cov10 { color: rgb(20, 236, 155) } | ||
|
||
</style> | ||
</head> | ||
<body> | ||
<div id="topbar"> | ||
<div id="nav"> | ||
<select id="files"> | ||
|
||
<option value="file0">github.com/cedrus-and-thuja/thyrsus-go/pkg/thyrsus/main.go (97.4%)</option> | ||
|
||
</select> | ||
</div> | ||
<div id="legend"> | ||
<span>not tracked</span> | ||
|
||
<span class="cov0">no coverage</span> | ||
<span class="cov1">low coverage</span> | ||
<span class="cov2">*</span> | ||
<span class="cov3">*</span> | ||
<span class="cov4">*</span> | ||
<span class="cov5">*</span> | ||
<span class="cov6">*</span> | ||
<span class="cov7">*</span> | ||
<span class="cov8">*</span> | ||
<span class="cov9">*</span> | ||
<span class="cov10">high coverage</span> | ||
|
||
</div> | ||
</div> | ||
<div id="content"> | ||
|
||
<pre class="file" id="file0" style="display: none">package thyrsus | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"slices" | ||
"testing" | ||
) | ||
|
||
var logger *log.Logger = log.New(os.Stderr, "", 0) | ||
|
||
var EmptyHeaders map[string]string = make(map[string]string, 0) | ||
|
||
type ExpectHttp struct { | ||
requests []*MockRequest | ||
request_log []*http.Request | ||
Errors []string | ||
testServer *httptest.Server | ||
} | ||
|
||
type MockResponse interface { | ||
Status() int | ||
Body() []byte | ||
Headers() map[string]string | ||
} | ||
|
||
type BaseMockResponse struct { | ||
status int | ||
body []byte | ||
headers map[string]string | ||
} | ||
|
||
func (res BaseMockResponse) Status() int <span class="cov7" title="4">{ | ||
return res.status | ||
}</span> | ||
|
||
func (res BaseMockResponse) Body() []byte <span class="cov7" title="4">{ | ||
return res.body | ||
}</span> | ||
|
||
func (res BaseMockResponse) Headers() map[string]string <span class="cov7" title="4">{ | ||
return res.headers | ||
}</span> | ||
|
||
type MockRequest struct { | ||
Url string | ||
Response MockResponse | ||
RequestHeaders map[string]string | ||
} | ||
|
||
func JSONMockResponse(status int, body interface{}, headers map[string]string) MockResponse <span class="cov9" title="6">{ | ||
data, err := json.Marshal(body) | ||
if err != nil </span><span class="cov1" title="1">{ | ||
data = []byte(err.Error()) | ||
}</span> | ||
<span class="cov9" title="6">headers["ContentType"] = "application/json" | ||
return MockResponse(BaseMockResponse{ | ||
status: status, | ||
body: data, | ||
headers: headers, | ||
})</span> | ||
} | ||
|
||
func NewExpectHttp() *ExpectHttp <span class="cov10" title="7">{ | ||
return &ExpectHttp{ | ||
requests: make([]*MockRequest, 0), | ||
request_log: make([]*http.Request, 0), | ||
} | ||
}</span> | ||
|
||
func (exp *ExpectHttp) ExpectRequest(url string, response MockResponse) <span class="cov7" title="4">{ | ||
exp.requests = append(exp.requests, &MockRequest{Url: url, Response: response}) | ||
}</span> | ||
|
||
func (exp *ExpectHttp) ExpectRequestWithHeaders(url string, response MockResponse, requestHeaders map[string]string) <span class="cov4" title="2">{ | ||
exp.requests = append(exp.requests, &MockRequest{Url: url, Response: response, RequestHeaders: requestHeaders}) | ||
}</span> | ||
|
||
func (exp *ExpectHttp) Start() string <span class="cov9" title="6">{ | ||
exp.testServer = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) </span><span class="cov10" title="7">{ | ||
exp.request_log = append(exp.request_log, r) | ||
if len(exp.requests) == 0 </span><span class="cov1" title="1">{ | ||
exp.Errors = append(exp.Errors, fmt.Sprintf("no more requests expected, but got %s", r.RequestURI)) | ||
w.WriteHeader(500) | ||
}</span> else<span class="cov9" title="6"> { | ||
expected := exp.requests[0] | ||
if expected.Url != r.RequestURI </span><span class="cov1" title="1">{ | ||
exp.Errors = append(exp.Errors, fmt.Sprintf("expected %s, but got %s", expected.Url, r.RequestURI)) | ||
w.WriteHeader(500) | ||
}</span> else<span class="cov8" title="5"> { | ||
exp.requests = exp.requests[1:] | ||
logger.Printf("request received: %s", r.RequestURI) | ||
for k, v := range expected.RequestHeaders </span><span class="cov4" title="2">{ | ||
if val, ok := r.Header[k]; !ok || !slices.Contains(val, v) </span><span class="cov1" title="1">{ | ||
exp.Errors = append(exp.Errors, fmt.Sprintf("expected %s header to contain %s, it did not", k, v)) | ||
w.WriteHeader(500) | ||
return | ||
}</span> | ||
} | ||
<span class="cov7" title="4">for k, v := range expected.Response.Headers() </span><span class="cov7" title="4">{ | ||
logger.Printf("setting response header: %s", k) | ||
w.Header().Set(k, v) | ||
}</span> | ||
<span class="cov7" title="4">w.WriteHeader(expected.Response.Status()) | ||
w.Write(expected.Response.Body())</span> | ||
} | ||
} | ||
})) | ||
<span class="cov9" title="6">exp.testServer.EnableHTTP2 = true | ||
exp.testServer.Start() | ||
return exp.testServer.URL</span> | ||
} | ||
|
||
func (exp *ExpectHttp) Close() <span class="cov9" title="6">{ | ||
exp.testServer.Close() | ||
}</span> | ||
|
||
func (exp *ExpectHttp) ValidateExpectations(T *testing.T) <span class="cov4" title="2">{ | ||
for _, err := range exp.Errors </span><span class="cov0" title="0">{ | ||
T.Error(err) | ||
}</span> | ||
} | ||
</pre> | ||
|
||
</div> | ||
</body> | ||
<script> | ||
(function() { | ||
var files = document.getElementById('files'); | ||
var visible; | ||
files.addEventListener('change', onChange, false); | ||
function select(part) { | ||
if (visible) | ||
visible.style.display = 'none'; | ||
visible = document.getElementById(part); | ||
if (!visible) | ||
return; | ||
files.value = part; | ||
visible.style.display = 'block'; | ||
location.hash = part; | ||
} | ||
function onChange() { | ||
select(files.value); | ||
window.scrollTo(0, 0); | ||
} | ||
if (location.hash != "") { | ||
select(location.hash.substr(1)); | ||
} | ||
if (!visible) { | ||
select("file0"); | ||
} | ||
})(); | ||
</script> | ||
</html> |
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
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
Oops, something went wrong.