Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0] Units #16

Merged
merged 4 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/stream/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,5 @@ func bidirectionalStream(c pb.GripmockClient, wg *sync.WaitGroup) {
log.Fatalf("2ds error: %v", err)
}
}
stream.CloseSend()
_ = stream.CloseSend()
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ go 1.21
require (
github.com/goccy/go-yaml v1.11.0
github.com/google/uuid v1.3.0
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/lithammer/fuzzysearch v1.1.8
github.com/stretchr/testify v1.8.4
github.com/tokopedia/gripmock/protogen v0.0.0
github.com/tokopedia/gripmock/protogen/example v0.0.0
golang.org/x/text v0.12.0
Expand All @@ -15,16 +17,18 @@ require (
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

// this is for generated server to be able to run
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
Expand Down Expand Up @@ -30,6 +32,10 @@ github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand Down Expand Up @@ -82,3 +88,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
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.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
11 changes: 6 additions & 5 deletions gripmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ func main() {
// for safety
output += "/"
if _, err := os.Stat(output); os.IsNotExist(err) {
os.Mkdir(output, os.ModePerm)
if err := os.Mkdir(output, os.ModePerm); err != nil {
log.Fatal(err)
}
}

// run admin stub server
Expand Down Expand Up @@ -71,9 +73,6 @@ func main() {
imports: importDirs,
})

// build the server
//buildServer(output)

// and run
run, runerr := runGrpcServer(output)

Expand All @@ -84,7 +83,9 @@ func main() {
log.Fatal(err)
case <-term:
fmt.Println("Stopping gRPC Server")
run.Process.Kill()
if err := run.Process.Kill(); err != nil {
log.Fatal(err)
}
}
}

Expand Down
52 changes: 36 additions & 16 deletions stub/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stub
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -39,7 +40,7 @@ func NewApiHandler() *ApiHandler {
return &ApiHandler{stubs: storage.New(), convertor: yaml2json.New()}
}

func (h *ApiHandler) searchHandle(w http.ResponseWriter, r *http.Request) {
func (h *ApiHandler) SearchHandle(w http.ResponseWriter, r *http.Request) {
stub := new(findStubPayload)
decoder := json.NewDecoder(r.Body)
decoder.UseNumber()
Expand All @@ -59,20 +60,21 @@ func (h *ApiHandler) searchHandle(w http.ResponseWriter, r *http.Request) {
output, err := findStub(h.stubs, stub)
if err != nil {
log.Println(err)
h.responseError(err, w)
w.WriteHeader(404)
h.writeResponseError(err, w)
return
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(output)
_ = json.NewEncoder(w).Encode(output)
}

func (h *ApiHandler) purgeHandle(w http.ResponseWriter, _ *http.Request) {
func (h *ApiHandler) PurgeHandle(w http.ResponseWriter, _ *http.Request) {
h.stubs.Purge()
w.WriteHeader(204)
}

func (h *ApiHandler) listHandle(w http.ResponseWriter, _ *http.Request) {
func (h *ApiHandler) ListHandle(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(h.stubs.Stubs())
if err != nil {
Expand All @@ -81,27 +83,39 @@ func (h *ApiHandler) listHandle(w http.ResponseWriter, _ *http.Request) {
}
}

func (h *ApiHandler) addHandle(w http.ResponseWriter, r *http.Request) {
// todo: add supported input array
stub := new(storage.Stub)
decoder := json.NewDecoder(r.Body)
decoder.UseNumber()

if err := decoder.Decode(stub); err != nil {
func (h *ApiHandler) AddHandle(w http.ResponseWriter, r *http.Request) {
byt, err := io.ReadAll(r.Body)
if err != nil {
h.responseError(err, w)
return
}

defer r.Body.Close()

if err := validateStub(stub); err != nil {
byt = bytes.TrimSpace(byt)

if byt[0] == '{' && byt[len(byt)-1] == '}' {
byt = []byte("[" + string(byt) + "]")
}

var stubs []*storage.Stub
decoder := json.NewDecoder(bytes.NewReader(byt))
decoder.UseNumber()

if err := decoder.Decode(&stubs); err != nil {
h.responseError(err, w)
return
}

for _, stub := range stubs {
if err := validateStub(stub); err != nil {
h.responseError(err, w)
return
}
}

w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(h.stubs.Add(stub))
if err != nil {
if err := json.NewEncoder(w).Encode(h.stubs.Add(stubs...)); err != nil {
h.responseError(err, w)
return
}
Expand All @@ -110,7 +124,13 @@ func (h *ApiHandler) addHandle(w http.ResponseWriter, r *http.Request) {
func (h *ApiHandler) responseError(err error, w http.ResponseWriter) {
w.WriteHeader(500)

_, _ = w.Write([]byte(err.Error()))
h.writeResponseError(err, w)
}

func (h *ApiHandler) writeResponseError(err error, w http.ResponseWriter) {
_ = json.NewEncoder(w).Encode(map[string]string{
"error": err.Error(),
})
}

func (h *ApiHandler) readStubs(path string) {
Expand Down
Loading