-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: additional usage examples (#18)
Addresses issues: - #16 - #15 Additionally: - Got rid of some linting errors from pre-commit hooks - Couple of instances of shadowing, file access etc, largely minor Co-authored-by: 0xste <[email protected]>
- Loading branch information
1 parent
cd806b2
commit 4e290f5
Showing
18 changed files
with
218 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.idea | ||
gocuke.iml | ||
gocuke.iml | ||
coverage.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Example - demonstrates REST API server implementation tests. | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/regen-network/gocuke" | ||
) | ||
|
||
// Server implements http.Server | ||
type Server struct { | ||
*http.Server | ||
} | ||
|
||
// NewServer creates a new instance of Server | ||
func NewServer(port int, handler http.Handler) *Server { | ||
addr := fmt.Sprintf(":%d", port) | ||
srv := &http.Server{ | ||
Addr: addr, | ||
Handler: handler, | ||
} | ||
return &Server{ | ||
Server: srv, | ||
} | ||
} | ||
|
||
func getVersion(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodGet { | ||
fail(w, "Method not allowed", http.StatusMethodNotAllowed) | ||
return | ||
} | ||
data := struct { | ||
Version string `json:"version"` | ||
}{Version: gocuke.Version} | ||
ok(w, data) | ||
} | ||
|
||
func main() { | ||
// Define your handler | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/version", getVersion) | ||
|
||
// Create a new server instance | ||
server := NewServer(8080, mux) | ||
|
||
// Start the server | ||
fmt.Printf("Server running on port %s...\n", server.Addr) | ||
if err := server.ListenAndServe(); err != nil { | ||
log.Fatalf("Server failed to start: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/regen-network/gocuke" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type suite struct { | ||
// special arguments like TestingT are injected automatically into exported fields | ||
gocuke.TestingT | ||
resp *httptest.ResponseRecorder | ||
} | ||
|
||
func TestApi(t *testing.T) { | ||
scope := &suite{TestingT: t, resp: httptest.NewRecorder()} | ||
gocuke.NewRunner(t, scope). | ||
Step(`^I send "(GET|POST|PUT|DELETE)" request to "([^"]*)"$`, scope.ISendRequestTo). | ||
Step(`^the response code should be (\d+)$`, scope.TheResponseCodeShouldBe). | ||
Step(`^the response should match json:$`, scope.TheResponseShouldMatchJson). | ||
Run() | ||
} | ||
|
||
func (s *suite) ISendRequestTo(method string, endpoint string) { | ||
req, err := http.NewRequest(method, endpoint, nil) | ||
assert.Nil(s, err) | ||
|
||
defer func() { | ||
switch t := recover().(type) { | ||
case string: | ||
err = fmt.Errorf(t) | ||
case error: | ||
err = t | ||
} | ||
}() | ||
|
||
switch endpoint { | ||
case "/version": | ||
getVersion(s.resp, req) | ||
default: | ||
err = fmt.Errorf("unknown endpoint: %s", endpoint) | ||
} | ||
assert.Nil(s, err) | ||
} | ||
|
||
func (s *suite) TheResponseCodeShouldBe(code int64) { | ||
assert.Equalf(s, code, int64(s.resp.Code), "expected response code to be: %d, but actual is: %d", code, s.resp.Code) | ||
} | ||
|
||
func (s *suite) TheResponseShouldMatchJson(body gocuke.DocString) { | ||
require.JSONEq(s, body.Content, s.resp.Body.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Feature: get version | ||
In order to know gocuke version | ||
As an API user | ||
I need to be able to request version | ||
|
||
Scenario: does not allow POST method | ||
When I send "POST" request to "/version" | ||
Then the response code should be 405 | ||
And the response should match json: | ||
""" | ||
{ | ||
"error": "Method not allowed" | ||
} | ||
""" | ||
|
||
Scenario: should get version number | ||
When I send "GET" request to "/version" | ||
Then the response code should be 200 | ||
And the response should match json: | ||
""" | ||
{ | ||
"version": "v0.0.0-dev" | ||
} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
// fail writes a json response with error msg and status header | ||
func fail(w http.ResponseWriter, msg string, status int) { | ||
w.WriteHeader(status) | ||
|
||
data := struct { | ||
Error string `json:"error"` | ||
}{Error: msg} | ||
resp, _ := json.Marshal(data) | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.Write(resp) | ||
} | ||
|
||
// ok writes data to response with 200 status | ||
func ok(w http.ResponseWriter, data interface{}) { | ||
resp, err := json.Marshal(data) | ||
if err != nil { | ||
fail(w, "Oops something evil has happened", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.Write(resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ Feature: simple | |
Examples: | ||
| x | y | z | | ||
| 5 | 3 | 2 | | ||
| 10 | 2 | 8 | | ||
| 10 | 2 | 8 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package gocuke | ||
|
||
import ( | ||
"runtime/debug" | ||
) | ||
|
||
// Version of package - based on Semantic Versioning 2.0.0 http://semver.org/ | ||
var Version = "v0.0.0-dev" | ||
|
||
func init() { | ||
if info, available := debug.ReadBuildInfo(); available { | ||
if Version == "v0.0.0-dev" && info.Main.Version != "(devel)" { | ||
Version = info.Main.Version | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.