Skip to content

Instantly share code, notes, and snippets.

func notFoundHandler(w http.ResponseWriter, r *http.Request) {
writeRawBody(w, r, notFoundResponse, http.StatusNotFound)
}
func doHandler() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req Request
if err := unmarshalBody(r, &req); err != nil {
log.Println(err)
response := NewErrorResponse(err)
func TestNotFoundHandler(t *testing.T) {
req := httptest.NewRequest("GET", "/not-found", nil)
rr := httptest.NewRecorder()
notFoundHandler(rr, req)
res := rr.Result()
resBody, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, http.StatusNotFound, res.StatusCode)
assert.JSONEq(t, `{"message": "not found"}`, string(resBody))
func remoteHandler(client HTTPClient) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
url := r.URL.Query().Get("url")
if url == "" {
url = "http://example.com/"
}
req, err := http.NewRequest(http.MethodPost, url, r.Body)
if err != nil {
log.Println(err)
func TestRemoteHandlerWithClientMock(t *testing.T) {
mockRes := &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader(`{"foo": "bar"}`)),
}
clientMock := mocks.HTTPClient{}
clientMock.On("Do", mock.AnythingOfType("*http.Request")).Return(mockRes, nil)
req := httptest.NewRequest("GET", "/remote", nil)
rr := httptest.NewRecorder()
func TestRemoteHandlerWithStubServer(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
defer ts.Close()
url := fmt.Sprintf("/remote?url=%s", ts.URL)
req := httptest.NewRequest("GET", url, nil)
rr := httptest.NewRecorder()
# 1. Build
FROM golang:alpine as builder
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
WORKDIR /build
version: '3'
services:
calc:
build: .
networks:
- calc-nw
ports:
- "${CALC_PORT:-8080}:8080"
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
writeRawBody(w, r, notFoundResponse, http.StatusNotFound)
}
func doHandler() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req Request
if err := unmarshalBody(r, &req); err != nil {
log.Println(err)
response := NewErrorResponse(err)
func Start(address string, routes map[string]http.HandlerFunc) error {
mux := http.NewServeMux()
for route, handler := range routes {
mux.Handle(route, handler)
}
s := &http.Server{
Addr: address,
Handler: mux,
{
"Records": [
{
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
"receiptHandle": "MessageReceiptHandle",
"body": "Hello from SQS!",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1523232000000",
"SenderId": "123456789012",