Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Go Httpbin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Nix
Go Httpbin
Commits
2362728a
Commit
2362728a
authored
8 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Getting started
parent
0081e767
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+17
-0
17 additions, 0 deletions
README.md
main.go
+96
-0
96 additions, 0 deletions
main.go
main_test.go
+49
-0
49 additions, 0 deletions
main_test.go
with
162 additions
and
0 deletions
README.md
0 → 100644
+
17
−
0
View file @
2362728a
# go-httpbin
A WIP golang port of https://httpbin.org/.
## Testing
```
go test
go test -cover
go test -coverprofile=cover.out && go tool cover -html=cover.out
```
## Running
```
go build && ./go-httpbin
```
This diff is collapsed.
Click to expand it.
main.go
0 → 100644
+
96
−
0
View file @
2362728a
package
main
import
(
"encoding/json"
"log"
"net/http"
"net/url"
)
// Resp is the standard JSON response from httpbin
type
Resp
struct
{
Args
url
.
Values
`json:"args"`
Headers
http
.
Header
`json:"headers"`
Origin
string
`json:"origin"`
URL
string
`json:"url"`
Data
string
`json:"data,omitempty"`
Files
map
[
string
][]
string
`json:"files,omitempty"`
Form
map
[
string
][]
string
`json:"form,omitempty"`
JSON
map
[
string
][]
string
`json:"json,omitempty"`
}
func
getOrigin
(
r
*
http
.
Request
)
string
{
origin
:=
r
.
Header
.
Get
(
"X-Forwarded-For"
)
if
origin
==
""
{
origin
=
r
.
RemoteAddr
}
return
origin
}
func
getURL
(
r
*
http
.
Request
)
string
{
scheme
:=
r
.
Header
.
Get
(
"X-Forwarded-Proto"
)
if
scheme
==
""
{
scheme
=
r
.
Header
.
Get
(
"X-Forwarded-Protocol"
)
}
if
scheme
==
""
&&
r
.
Header
.
Get
(
"X-Forwarded-Ssl"
)
==
"on"
{
scheme
=
"https"
}
if
scheme
==
""
{
scheme
=
"http"
}
host
:=
r
.
URL
.
Host
if
host
==
""
{
host
=
r
.
Host
}
u
:=
&
url
.
URL
{
Scheme
:
scheme
,
Opaque
:
r
.
URL
.
Opaque
,
User
:
r
.
URL
.
User
,
Host
:
host
,
Path
:
r
.
URL
.
Path
,
RawPath
:
r
.
URL
.
RawPath
,
ForceQuery
:
r
.
URL
.
ForceQuery
,
RawQuery
:
r
.
URL
.
RawQuery
,
Fragment
:
r
.
URL
.
Fragment
,
}
return
u
.
String
()
}
func
writeResponse
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
resp
*
Resp
)
{
resp
.
Origin
=
getOrigin
(
r
)
resp
.
URL
=
getURL
(
r
)
body
,
err
:=
json
.
Marshal
(
resp
)
if
err
!=
nil
{
log
.
Printf
(
"error marshalling %v as JSON: %s"
,
resp
,
err
)
}
w
.
Write
(
body
)
}
func
logger
(
h
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
h
.
ServeHTTP
(
w
,
r
)
log
.
Printf
(
"%s %s %s"
,
r
.
RemoteAddr
,
r
.
Method
,
r
.
URL
)
})
}
func
get
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
r
.
ParseForm
()
resp
:=
&
Resp
{
Args
:
r
.
Form
,
Headers
:
r
.
Header
,
}
writeResponse
(
w
,
r
,
resp
)
}
func
main
()
{
h
:=
http
.
NewServeMux
()
h
.
HandleFunc
(
"/get"
,
get
)
log
.
Printf
(
"listening on 9999"
)
err
:=
http
.
ListenAndServe
(
":9999"
,
logger
(
h
))
log
.
Fatal
(
err
)
}
This diff is collapsed.
Click to expand it.
main_test.go
0 → 100644
+
49
−
0
View file @
2362728a
package
main
import
(
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func
TestGet
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/get"
,
nil
)
r
.
Host
=
"localhost"
r
.
Header
.
Set
(
"User-Agent"
,
"test"
)
w
:=
httptest
.
NewRecorder
()
get
(
w
,
r
)
if
w
.
Code
!=
200
{
t
.
Fatalf
(
"expected status code 200, got %d"
,
w
.
Code
)
}
var
resp
*
Resp
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
}
if
resp
.
Args
.
Encode
()
!=
""
{
t
.
Fatalf
(
"expected empty args, got %s"
,
resp
.
Args
.
Encode
())
}
if
resp
.
Origin
!=
""
{
t
.
Fatalf
(
"expected empty origin, got %#v"
,
resp
.
Origin
)
}
if
resp
.
URL
!=
"http://localhost/get"
{
t
.
Fatalf
(
"unexpected url: %#v"
,
resp
.
URL
)
}
var
headerTests
=
[]
struct
{
key
string
expected
string
}{
{
"Content-Type"
,
""
},
{
"User-Agent"
,
"test"
},
}
for
_
,
test
:=
range
headerTests
{
if
resp
.
Headers
.
Get
(
test
.
key
)
!=
test
.
expected
{
t
.
Fatalf
(
"expected %s = %#v, got %#v"
,
test
.
key
,
test
.
expected
,
resp
.
Headers
.
Get
(
test
.
key
))
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment