Permalink
Browse files

fix #187: remove context injection

  • Loading branch information...
kamilsk committed Dec 31, 2018
1 parent 12f4028 commit b50719324164bcf8418df7236d763af84d188cf4
@@ -25,6 +25,15 @@ code-quality-check: docker-tool-gometalinter
code-quality-report:
time make code-quality-check | tail +7 | tee report.out

.PHONY: goimports-check
goimports-check:
@(goimports -d ./cmd/ ./pkg/ \
| grep '.go$$' \
| grep -v mock_ \
| grep -v _easyjson \
| grep -v bindata \
| awk '{print $$4}')


.PHONY: dev
dev: up stop-server stop-service clear status demo dev-server
@@ -10,18 +10,17 @@ import (
"strings"
"time"

pb "github.com/kamilsk/form-api/pkg/server/grpc"
kit "github.com/kamilsk/go-kit/pkg/strings"

"github.com/kamilsk/form-api/pkg/config"
"github.com/kamilsk/form-api/pkg/domain"
pb "github.com/kamilsk/form-api/pkg/server/grpc"
"github.com/kamilsk/form-api/pkg/server/grpc/middleware"
kit "github.com/kamilsk/go-kit/pkg/strings"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)

const (
@@ -11,7 +11,7 @@ import (
"github.com/kamilsk/form-api/pkg/static"
"github.com/kamilsk/form-api/pkg/storage"
"github.com/kamilsk/go-kit/pkg/fn"
"github.com/rubenv/sql-migrate"
migrate "github.com/rubenv/sql-migrate"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@@ -9,11 +9,10 @@ import (
"runtime"
"strconv"

pb "github.com/kamilsk/form-api/pkg/server/grpc"

"github.com/grpc-ecosystem/go-grpc-middleware/auth"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
"github.com/kamilsk/form-api/pkg/config"
"github.com/kamilsk/form-api/pkg/server"
pb "github.com/kamilsk/form-api/pkg/server/grpc"
"github.com/kamilsk/form-api/pkg/server/grpc/middleware"
"github.com/kamilsk/form-api/pkg/server/router/chi"
"github.com/kamilsk/form-api/pkg/service"
38 go.mod

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
445 go.sum

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -8,7 +8,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"

. "github.com/kamilsk/form-api/pkg/config"
)
@@ -9,7 +9,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"

. "github.com/kamilsk/form-api/pkg/domain"
)
@@ -3,7 +3,7 @@ package server
import (
"context"

"github.com/kamilsk/form-api/pkg/transfer/api/v1"
v1 "github.com/kamilsk/form-api/pkg/transfer/api/v1"
)

// Service defines the behavior of the Forma service.
@@ -3,7 +3,7 @@ package middleware
import (
"context"

"github.com/grpc-ecosystem/go-grpc-middleware/auth"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
"github.com/kamilsk/form-api/pkg/domain"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
@@ -1,3 +1,3 @@
//go:generate echo $PWD/$GOPACKAGE/$GOFILE
//go:generate mockgen -package grpc_test -destination $PWD/pkg/server/grpc/mock_storage_test.go github.com/kamilsk/form-api/pkg/server/grpc ProtectedStorage
//go:generate echo $PWD - $GOPACKAGE - $GOFILE
//go:generate mockgen -package grpc_test -destination mock_storage_test.go github.com/kamilsk/form-api/pkg/server/grpc ProtectedStorage
package grpc_test
@@ -3,8 +3,4 @@ package middleware
type (
// EncoderKey is used as a context key to store a Schema encoder.
EncoderKey struct{}
// SchemaKey is used as a context key to store a Schema ID.
SchemaKey struct{}
// TemplateKey is used as a context key to store a Template ID.
TemplateKey struct{}
)

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,25 @@
package middleware

import (
"net/http"

"github.com/go-chi/chi"
)

// Pack gets params from URL and puts it to a query.
func Pack(args ...string) func(http.Handler) http.Handler {
if len(args) == 0 || len(args)%2 != 0 {
panic("passed arguments must contain from => to list")
}
return func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if err := req.ParseForm(); err == nil {
for i, steps := 0, len(args)/2; i < steps; i++ {
from, to := args[i], args[i+1]
req.Form.Set(to, chi.URLParam(req, from))
}
}
handler.ServeHTTP(rw, req)
})
}
}
@@ -0,0 +1,60 @@
package middleware_test

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/go-chi/chi"
"github.com/kamilsk/form-api/pkg/server/router/chi/middleware"
"github.com/stretchr/testify/assert"
)

const uuid = "10000000-2000-4000-8000-160000000004"

func TestPack(t *testing.T) {
tests := []struct {
name string
from string
to string
ctx func() *chi.Context
expected string
}{
{"present", "ID", "id", func() *chi.Context {
ctx := chi.NewRouteContext()
ctx.URLParams.Add("ID", uuid)
return ctx
}, uuid},
{"not present", "ID", "id", chi.NewRouteContext, ""},
}
for _, test := range tests {
tc := test
t.Run("usual: "+test.name, func(t *testing.T) {
ctx := context.WithValue(context.TODO(), chi.RouteCtxKey, tc.ctx())
handler := middleware.Pack(tc.from, tc.to)(
http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
assert.Equal(t, tc.expected, req.Form.Get(tc.to))
}),
)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/{%s}", tc.from), nil)
assert.NoError(t, err)
handler.ServeHTTP(nil, req.WithContext(ctx))
})
}

panics := []struct {
name string
args []string
}{
{"nil args", nil},
{"empty args", []string{}},
{"odd args", []string{"ID", "id", "URL"}},
}
for _, test := range panics {
tc := test
t.Run("panic: "+test.name, func(t *testing.T) {
assert.Panics(t, func() { middleware.Pack(tc.args...) })
})
}
}
Oops, something went wrong.

0 comments on commit b507193

Please sign in to comment.