Skip to content

Commit

Permalink
Migrate to mux by default
Browse files Browse the repository at this point in the history
  • Loading branch information
prongbang committed Mar 30, 2022
1 parent 4e698cd commit 9f2c2e6
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 195 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mock
mock
Dockerfile
38 changes: 11 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@

> [Wiremock](https://hub.docker.com/r/prongbang/wiremock) Minimal Mock your APIs
## How to run

### Run with Docker

#### Version 1.0.+

Support matching routes with [gorilla/mux](https://github.com/gorilla/mux#matching-routes)
```shell script
_ ___ __
| | /| / (_)______ __ _ ___ ____/ /__
| |/ |/ / / __/ -_) ' \/ _ \/ __/ '_/
|__/|__/_/_/ \__/_/_/_/\___/\__/_/\_\

```shell
docker pull prongbang/wiremock:1.3.1
-> wiremock server started on :8000
```

#### Version 2.0.+

Support matching routes with [gofiber/fiber](https://docs.gofiber.io/guide/routing)
### Run with Docker

```shell
docker pull prongbang/wiremock:2.0.1
docker pull prongbang/wiremock:latest
```

### Run with Docker Compose
Expand Down Expand Up @@ -58,24 +53,13 @@ $ wiremock
$ wiremock -port=9000
```

- Running

```shell script
_ ___ __
| | /| / (_)______ __ _ ___ ____/ /__
| |/ |/ / / __/ -_) ' \/ _ \/ __/ '_/
|__/|__/_/_/ \__/_/_/_/\___/\__/_/\_\

-> wiremock server started on :8000
```

### Example Project

[https://github.com/prongbang/wiremock-example](https://github.com/prongbang/wiremock-example)

## Matching Routes using gofiber/fiber
## Matching Routes using gorilla/mux

Read doc [https://docs.gofiber.io/guide/routing](https://docs.gofiber.io/guide/routing)
Read doc [gorilla/mux](https://github.com/gorilla/mux#matching-routes)

## Setup project

Expand Down Expand Up @@ -137,7 +121,7 @@ routes:
get_user:
request:
method: "GET"
url: "/api/v1/user/:id"
url: "/api/v1/user/{id:[0-9]+}"
response:
status: 200
body_file: user.json
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ module github.com/prongbang/wiremock/v2
go 1.16

require (
github.com/gofiber/fiber/v2 v2.27.0
github.com/klauspost/compress v1.14.3 // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
gopkg.in/yaml.v2 v2.4.0
)
32 changes: 6 additions & 26 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/gofiber/fiber/v2 v2.27.0 h1:u34t1nOea7zz4jcZDK7+ZMiG+MVFYrHqMhTdYQDiFA8=
github.com/gofiber/fiber/v2 v2.27.0/go.mod h1:0bPXdTu+jRqINrEq1T6mHeVBnE0lQd67PGu35jD3hLk=
github.com/klauspost/compress v1.14.1/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.14.3 h1:DQv1WP+iS4srNjibdnHtqu8JNWCDMluj5NzPnFJsnvk=
github.com/klauspost/compress v1.14.3/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.33.0 h1:mHBKd98J5NcXuBddgjvim1i3kWzlng1SzLhrnBOU9g8=
github.com/valyala/fasthttp v1.33.0/go.mod h1:KJRK/MXx0J+yd0c5hlR+s1tIHD72sniU8ZJjl97LIw4=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220111093109-d55c255bac03/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
40 changes: 10 additions & 30 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package api

import (
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"strings"

"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/prongbang/wiremock/v2/pkg/config"
"github.com/prongbang/wiremock/v2/pkg/status"
"log"
"net/http"
)

type API interface {
Expand All @@ -21,38 +21,18 @@ type api struct {
func (a *api) Register(cfg config.Config) {
status.Banner()

conf := fiber.Config{
DisableStartupMessage: true,
}
app := fiber.New(conf)

// Middleware
app.Use(cors.New(cors.Config{
AllowOrigins: "*",
AllowHeaders: "*",
AllowMethods: strings.Join([]string{
fiber.MethodGet,
fiber.MethodPost,
fiber.MethodHead,
fiber.MethodPut,
fiber.MethodDelete,
fiber.MethodPatch,
fiber.MethodOptions,
fiber.MethodTrace,
fiber.MethodConnect,
}, ","),
}))
r := mux.NewRouter()
headers := handlers.AllowedHeaders([]string{"*"})
methods := handlers.AllowedMethods([]string{http.MethodGet, http.MethodPost, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodTrace, http.MethodDelete, http.MethodOptions})
origins := handlers.AllowedOrigins([]string{"*"})

// Router
a.Router.Initials(app)
a.Router.Initials(r)

status.Started(cfg.Port)

// Listening
err := app.Listen(fmt.Sprintf(":%s", cfg.Port))
if err != nil {
panic(err)
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", cfg.Port), handlers.CORS(headers, methods, origins)(r)))
}

// NewAPI provide apis
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/home/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package home

import (
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/prongbang/wiremock/v2/pkg/config"
"net/http"
)

type Handler interface {
GetHome(c *fiber.Ctx) error
GetHome(w http.ResponseWriter, r *http.Request)
}

type handler struct {
Cfg config.Config
}

func (h *handler) GetHome(c *fiber.Ctx) error {
return c.SendString(fmt.Sprintf("Wiremock server started on %s", h.Cfg.Port))
func (h *handler) GetHome(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, "Wiremock server started on "+h.Cfg.Port)
}

func NewHandler(cfg config.Config) Handler {
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/home/router.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package home

import (
"github.com/gofiber/fiber/v2"
"github.com/gorilla/mux"
)

type Router interface {
Initial(app *fiber.App)
Initial(route *mux.Router)
}

type router struct {
Handle Handler
}

func (r *router) Initial(app *fiber.App) {
app.Get("/", r.Handle.GetHome)
func (r *router) Initial(route *mux.Router) {
route.HandleFunc("/", r.Handle.GetHome).Methods("GET")
}

func NewRouter(handle Handler) Router {
Expand Down
10 changes: 5 additions & 5 deletions pkg/api/routers.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package api

import (
"github.com/gofiber/fiber/v2"
"github.com/gorilla/mux"
"github.com/prongbang/wiremock/v2/pkg/api/home"
"github.com/prongbang/wiremock/v2/pkg/api/wiremock"
)

type Routers interface {
Initials(app *fiber.App)
Initials(route *mux.Router)
}

type routers struct {
HomeRoute home.Router
WiremockRoute wiremock.Router
}

func (r *routers) Initials(app *fiber.App) {
r.HomeRoute.Initial(app)
r.WiremockRoute.Initial(app)
func (r *routers) Initials(route *mux.Router) {
r.HomeRoute.Initial(route)
r.WiremockRoute.Initial(route)
}

func NewRouters(
Expand Down
37 changes: 23 additions & 14 deletions pkg/api/wiremock/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,44 @@ package wiremock

import (
"fmt"
"github.com/gofiber/fiber/v2"
"net/http"

"github.com/prongbang/wiremock/v2/pkg/core"
)

// Handler is a model for handler router
type Handler interface {
Handle(c *fiber.Ctx) error
Handle(w http.ResponseWriter, r *http.Request)
}

type handler struct {
UseCase UseCase
Routers Routers
}

func (h *handler) Handle(c *fiber.Ctx) error {
func (h *handler) Handle(w http.ResponseWriter, r *http.Request) {
// Reading form values
maxMemory := 32 << 20 // 32Mb
if err := r.ParseMultipartForm(int64(maxMemory)); err != nil {
_ = r.ParseForm()
}

// Prepared request
httpHeader := core.BindHeader(h.Routers.Request.Header, c)
httpHeader := core.BindHeader(h.Routers.Request.Header, r)

// Prepared response
if len(h.Routers.Response.Header) == 0 {
c.Response().Header.Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json")
}
for k, v := range h.Routers.Response.Header {
c.Set(k, fmt.Sprintf("%v", v))
w.Header().Set(k, fmt.Sprintf("%v", v))
}

// Process cases matching
if len(h.Routers.Request.Cases) > 0 {

// Process cases matching
matching := h.UseCase.CasesMatching(c, h.Routers.Response.FileName, h.Routers.Request.Cases, Parameters{
matching := h.UseCase.CasesMatching(r, h.Routers.Response.FileName, h.Routers.Request.Cases, Parameters{
ReqHeader: ReqHeader{
HttpHeader: httpHeader,
MockHeader: h.Routers.Request.Header,
Expand All @@ -45,14 +49,17 @@ func (h *handler) Handle(c *fiber.Ctx) error {
// Process response
if matching.IsMatch {
response := h.UseCase.GetMockResponse(matching.Case.Response)
return c.Status(matching.Case.Response.Status).SendString(string(response))
w.WriteHeader(matching.Case.Response.Status)
_, _ = w.Write(response)
} else {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write(matching.Result)
}

return c.Status(http.StatusBadRequest).SendString(string(matching.Result))
return
}

// Prepared request
body := core.BindBody(h.Routers.Request.Body, c)
body := core.BindBody(h.Routers.Request.Body, r)

// Process parameter matching
matching := h.UseCase.ParameterMatching(Parameters{
Expand All @@ -69,10 +76,12 @@ func (h *handler) Handle(c *fiber.Ctx) error {
// Prepared response
if matching.IsMatch {
response := h.UseCase.GetMockResponse(h.Routers.Response)
return c.Status(h.Routers.Response.Status).SendString(string(response))
w.WriteHeader(h.Routers.Response.Status)
_, _ = w.Write(response)
} else {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write(matching.Result)
}

return c.Status(http.StatusBadRequest).SendString(string(matching.Result))
}

// NewHandler a instance
Expand Down
Loading

0 comments on commit 9f2c2e6

Please sign in to comment.