Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Łukasz Nowak
caddy
Commits
7dadcd58
Commit
7dadcd58
authored
Dec 30, 2015
by
Abiola Ibrahim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add ability to set custom values.
parent
73327e78
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
0 deletions
+46
-0
middleware/replacer.go
middleware/replacer.go
+6
-0
middleware/replacer_test.go
middleware/replacer_test.go
+40
-0
No files found.
middleware/replacer.go
View file @
7dadcd58
...
...
@@ -16,6 +16,7 @@ import (
// NewReplacer to get one of these.
type
Replacer
interface
{
Replace
(
string
)
string
Set
(
key
,
value
string
)
}
type
replacer
struct
{
...
...
@@ -117,6 +118,11 @@ func (r replacer) Replace(s string) string {
return
s
}
// Set sets key to value in the replacements map.
func
(
r
replacer
)
Set
(
key
,
value
string
)
{
r
.
replacements
[
"{"
+
key
+
"}"
]
=
value
}
const
(
timeFormat
=
"02/Jan/2006:15:04:05 -0700"
headerReplacer
=
"{>"
...
...
middleware/replacer_test.go
View file @
7dadcd58
...
...
@@ -69,3 +69,43 @@ func TestReplace(t *testing.T) {
}
}
func
TestSet
(
t
*
testing
.
T
)
{
w
:=
httptest
.
NewRecorder
()
recordRequest
:=
NewResponseRecorder
(
w
)
userJSON
:=
`{"username": "dennis"}`
reader
:=
strings
.
NewReader
(
userJSON
)
//Convert string to reader
request
,
err
:=
http
.
NewRequest
(
"POST"
,
"http://caddyserver.com"
,
reader
)
//Create request with JSON body
if
err
!=
nil
{
t
.
Fatalf
(
"Request Formation Failed
\n
"
)
}
replaceValues
:=
NewReplacer
(
request
,
recordRequest
,
""
)
replaceValues
.
Set
(
"host"
,
"getcaddy.com"
)
replaceValues
.
Set
(
"method"
,
"GET"
)
replaceValues
.
Set
(
"status"
,
"201"
)
replaceValues
.
Set
(
"variable"
,
"value"
)
switch
v
:=
replaceValues
.
(
type
)
{
case
replacer
:
if
v
.
Replace
(
"This host is {host}"
)
!=
"This host is getcaddy.com"
{
t
.
Errorf
(
"Expected host replacement failed"
)
}
if
v
.
Replace
(
"This request method is {method}"
)
!=
"This request method is GET"
{
t
.
Errorf
(
"Expected method replacement failed"
)
}
if
v
.
Replace
(
"The response status is {status}"
)
!=
"The response status is 201"
{
t
.
Errorf
(
"Expected status replacement failed"
)
}
if
v
.
Replace
(
"The value of variable is {variable}"
)
!=
"The value of variable is value"
{
t
.
Errorf
(
"Expected status replacement failed"
)
}
default
:
t
.
Fatalf
(
"Return Value from New Replacer expected pass type assertion into a replacer type
\n
"
)
}
}
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