Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
b6057adb
Commit
b6057adb
authored
Oct 16, 2014
by
Dawn Chen
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1822 from lavalamp/eventing3
Add lock to fake handler to avoid races.
parents
9edd8a10
15c50129
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
1 deletion
+24
-1
fake_handler.go
pkg/util/fake_handler.go
+22
-1
fake_handler_test.go
pkg/util/fake_handler_test.go
+2
-0
No files found.
pkg/util/fake_handler.go
View file @
b6057adb
...
@@ -21,12 +21,14 @@ import (
...
@@ -21,12 +21,14 @@ import (
"net/http"
"net/http"
"net/url"
"net/url"
"reflect"
"reflect"
"sync"
)
)
// TestInterface is a simple interface providing Errorf, to make injection for
// TestInterface is a simple interface providing Errorf, to make injection for
// testing easier (insert 'yo dawg' meme here).
// testing easier (insert 'yo dawg' meme here).
type
TestInterface
interface
{
type
TestInterface
interface
{
Errorf
(
format
string
,
args
...
interface
{})
Errorf
(
format
string
,
args
...
interface
{})
Logf
(
format
string
,
args
...
interface
{})
}
}
// LogInterface is a simple interface to allow injection of Logf to report serving errors.
// LogInterface is a simple interface to allow injection of Logf to report serving errors.
...
@@ -45,9 +47,21 @@ type FakeHandler struct {
...
@@ -45,9 +47,21 @@ type FakeHandler struct {
// For logging - you can use a *testing.T
// For logging - you can use a *testing.T
// This will keep log messages associated with the test.
// This will keep log messages associated with the test.
T
LogInterface
T
LogInterface
// Enforce "only one use" constraint.
lock
sync
.
Mutex
requestCount
int
hasBeenChecked
bool
}
}
func
(
f
*
FakeHandler
)
ServeHTTP
(
response
http
.
ResponseWriter
,
request
*
http
.
Request
)
{
func
(
f
*
FakeHandler
)
ServeHTTP
(
response
http
.
ResponseWriter
,
request
*
http
.
Request
)
{
f
.
lock
.
Lock
()
defer
f
.
lock
.
Unlock
()
f
.
requestCount
++
if
f
.
hasBeenChecked
{
panic
(
"got request after having been validated"
)
}
f
.
RequestReceived
=
request
f
.
RequestReceived
=
request
response
.
WriteHeader
(
f
.
StatusCode
)
response
.
WriteHeader
(
f
.
StatusCode
)
response
.
Write
([]
byte
(
f
.
ResponseBody
))
response
.
Write
([]
byte
(
f
.
ResponseBody
))
...
@@ -60,7 +74,14 @@ func (f *FakeHandler) ServeHTTP(response http.ResponseWriter, request *http.Requ
...
@@ -60,7 +74,14 @@ func (f *FakeHandler) ServeHTTP(response http.ResponseWriter, request *http.Requ
}
}
// ValidateRequest verifies that FakeHandler received a request with expected path, method, and body.
// ValidateRequest verifies that FakeHandler received a request with expected path, method, and body.
func
(
f
FakeHandler
)
ValidateRequest
(
t
TestInterface
,
expectedPath
,
expectedMethod
string
,
body
*
string
)
{
func
(
f
*
FakeHandler
)
ValidateRequest
(
t
TestInterface
,
expectedPath
,
expectedMethod
string
,
body
*
string
)
{
f
.
lock
.
Lock
()
defer
f
.
lock
.
Unlock
()
if
f
.
requestCount
!=
1
{
t
.
Logf
(
"Expected 1 call, but got %v. Only the last call is recorded and checked."
,
f
.
requestCount
)
}
f
.
hasBeenChecked
=
true
expectURL
,
err
:=
url
.
Parse
(
expectedPath
)
expectURL
,
err
:=
url
.
Parse
(
expectedPath
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"Couldn't parse %v as a URL."
,
expectedPath
)
t
.
Errorf
(
"Couldn't parse %v as a URL."
,
expectedPath
)
...
...
pkg/util/fake_handler_test.go
View file @
b6057adb
...
@@ -72,6 +72,8 @@ func (f *fakeError) Errorf(format string, args ...interface{}) {
...
@@ -72,6 +72,8 @@ func (f *fakeError) Errorf(format string, args ...interface{}) {
f
.
errors
=
append
(
f
.
errors
,
format
)
f
.
errors
=
append
(
f
.
errors
,
format
)
}
}
func
(
f
*
fakeError
)
Logf
(
format
string
,
args
...
interface
{})
{}
func
TestFakeHandlerWrongPath
(
t
*
testing
.
T
)
{
func
TestFakeHandlerWrongPath
(
t
*
testing
.
T
)
{
handler
:=
FakeHandler
{}
handler
:=
FakeHandler
{}
server
:=
httptest
.
NewServer
(
&
handler
)
server
:=
httptest
.
NewServer
(
&
handler
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment