Skip to content

Commit

Permalink
Merge pull request #68 from apitoolkit/fix_tests
Browse files Browse the repository at this point in the history
fix test
  • Loading branch information
dawkaka committed Apr 9, 2024
2 parents 7dbc2b0 + bf67269 commit 7cb2a3d
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 101 deletions.
55 changes: 27 additions & 28 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,58 @@ name: Go

on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
branches: ["main"]

jobs:
test:
name: Test
strategy:
fail-fast: true
matrix:
go: ['1.20', '1.21']
os: ['ubuntu-latest']
go: ["1.20", "1.21"]
os: ["ubuntu-latest"]
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
- uses: actions/checkout@v3

- name: Format
run: gofmt -s -w . && git diff --exit-code
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}

- name: Vet
run: go vet ./...
- name: Format
run: gofmt -s -w . && git diff --exit-code

- name: Tidy
run: go mod tidy && git diff --exit-code
- name: Vet
run: go vet ./...

- name: Mod
run: go mod download
- name: Tidy
run: go mod tidy && git diff --exit-code

- name: Mod Verify
run: go mod verify
- name: Mod
run: go mod download

- name: Generate
run: go generate ./... && git diff --exit-code
- name: Mod Verify
run: go mod verify

- name: Test
run: go test -v -count=1 -race -shuffle=on -cover ./...
env:
APITOOLKIT_KEY: ${{ secrets.APITOOLKIT_KEY }}
- name: Generate
run: go generate ./... && git diff --exit-code

- name: Test
run: go test
env:
APITOOLKIT_KEY: ${{ secrets.APITOOLKIT_KEY }}

build:
name: Build
strategy:
fail-fast: true
matrix:
go: ['1.20', '1.21']
os: ['ubuntu-latest']
go: ["1.20", "1.21"]
os: ["ubuntu-latest"]
runs-on: ${{ matrix.os }}

steps:
Expand Down
4 changes: 4 additions & 0 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"context"
"errors"
"io"
"net"
"net/http"
Expand Down Expand Up @@ -72,6 +73,9 @@ func (c *Client) EchoMiddleware(next echo.HandlerFunc) echo.HandlerFunc {

defer func() {
if err := recover(); err != nil {
if _, ok := err.(error); !ok {
err = errors.New(err.(string))
}
ReportError(ctx.Request().Context(), err.(error))
payload := c.buildPayload(GoDefaultSDKType, startTime,
ctx.Request(), 500,
Expand Down
3 changes: 0 additions & 3 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/gin-gonic/gin"
"github.com/imroc/req"
"github.com/kr/pretty"
"github.com/stretchr/testify/assert"
)

Expand All @@ -28,7 +27,6 @@ func TestErrorReporting(t *testing.T) {
client.PublishMessage = func(ctx context.Context, payload Payload) error {
// x, _ := json.MarshalIndent(payload, "", "\t")
// fmt.Println(string(x))
pretty.Println(payload.Errors)
assert.NotEmpty(t, payload.Errors)
assert.Equal(t, "wrapper from err2 Example Error value", payload.Errors[0].Message)
assert.Equal(t, "Example Error value", payload.Errors[0].RootErrorMessage)
Expand Down Expand Up @@ -87,7 +85,6 @@ func TestGinMiddlewareGETError(t *testing.T) {
respData, _ := json.Marshal(exampleData)
client.PublishMessage = func(ctx context.Context, payload Payload) error {
publishCalled = true
pretty.Println(payload)
return nil
}
router := gin.New()
Expand Down
3 changes: 1 addition & 2 deletions fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func (c *Client) FiberMiddleware(ctx *fiber.Ctx) error {
ctx.Locals(string(CurrentRequestMessageID), msgID)
errorList := []ATError{}
ctx.Locals(string(ErrorListCtxKey), &errorList)
ctx.Locals(CurrentClient, c)
newCtx := context.WithValue(ctx.Context(), ErrorListCtxKey, &errorList)
newCtx = context.WithValue(newCtx, CurrentClient, c)
newCtx = context.WithValue(newCtx, CurrentRequestMessageID, msgID)
ctx.SetUserContext(newCtx)

respHeaders := map[string][]string{}
for k, v := range ctx.GetRespHeaders() {
respHeaders[k] = v
Expand Down Expand Up @@ -51,7 +51,6 @@ func (c *Client) FiberMiddleware(ctx *fiber.Ctx) error {
}()

err := ctx.Next()

payload := c.buildFastHTTPPayload(GoFiberSDKType, start,
ctx.Context(), ctx.Response().StatusCode(),
ctx.Request().Body(), ctx.Response().Body(), respHeaders,
Expand Down
10 changes: 3 additions & 7 deletions fiber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
)

func TestFiberMiddleware(t *testing.T) {
t.Skip()
client := &Client{
config: &Config{
RedactHeaders: []string{"X-Api-Key", "Accept-Encoding"},
Expand Down Expand Up @@ -43,10 +42,7 @@ func TestFiberMiddleware(t *testing.T) {
"X-Api-Key": {"past-3"},
"Host": {"example.com"},
}, payload.RequestHeaders)
assert.Equal(t, map[string][]string{
"Content-Type": {"application/json"},
"X-Api-Key": {"applicationKey"},
}, payload.ResponseHeaders)
assert.Equal(t, map[string][]string{"Content-Type": {"text/plain; charset=utf-8"}}, payload.ResponseHeaders)
assert.Equal(t, "/slug-value/test?param1=abc&param2=123", payload.RawURL)
assert.Equal(t, http.StatusAccepted, payload.StatusCode)
assert.Greater(t, payload.Duration, 1000*time.Nanosecond)
Expand All @@ -69,8 +65,8 @@ func TestFiberMiddleware(t *testing.T) {
reqData, _ := json.Marshal(exampleData2)
assert.Equal(t, reqData, body)

c.Append("Content-Type", "application/json")
c.Append("X-API-KEY", "applicationKey")
c.Set("Content-Type", "application/json")
c.Set("X-API-KEY", "applicationKey")

return c.Status(http.StatusAccepted).JSON(exampleData)
})
Expand Down
4 changes: 4 additions & 0 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package apitoolkit
import (
"bytes"
"context"
"errors"
"io"
"time"

Expand Down Expand Up @@ -53,6 +54,9 @@ func (c *Client) GinMiddleware(ctx *gin.Context) {

defer func() {
if err := recover(); err != nil {
if _, ok := err.(error); !ok {
err = errors.New(err.(string))
}
ReportError(ctx.Request.Context(), err.(error))
payload := c.buildPayload(GoGinSDKType, start,
ctx.Request, 500,
Expand Down
6 changes: 3 additions & 3 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func TestReportingInteg(t *testing.T) {
Tags: []string{"staging"},
}
client, err := NewClient(ctx, cfg)
if err != nil {
panic(err)
}
defer client.Close()
assert.NoError(t, err)

Expand All @@ -34,12 +37,9 @@ func TestReportingInteg(t *testing.T) {
err2 := errors.Wrap(err1, "wrapper from err2")
ReportError(r.Context(), err2)
}

ts := httptest.NewServer(client.Middleware(http.HandlerFunc(handlerFn)))
defer ts.Close()

atHTTPClient := HTTPClient(ctx)
req.SetClient(atHTTPClient)
_, err = req.Post(ts.URL+"/test",
req.Param{"param1": "abc", "param2": 123},
req.Header{
Expand Down
1 change: 1 addition & 0 deletions native.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (c *Client) GorillaMuxMiddleware(next http.Handler) http.Handler {

errorList := []ATError{}
newCtx = context.WithValue(newCtx, ErrorListCtxKey, &errorList)
newCtx = context.WithValue(newCtx, CurrentClient, c)
req = req.WithContext(newCtx)

reqBuf, _ := io.ReadAll(req.Body)
Expand Down
4 changes: 3 additions & 1 deletion outgoing.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ type roundTripper struct {

func (rt *roundTripper) RoundTrip(req *http.Request) (res *http.Response, err error) {
defer func() {
ReportError(rt.ctx, err)
if err != nil {
ReportError(rt.ctx, err)
}
}()

if rt.client == nil {
Expand Down
9 changes: 7 additions & 2 deletions outgoing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func TestReporting(t *testing.T) {
Tags: []string{"staging"},
}
client, err := NewClient(ctx, cfg)
if err != nil {
panic(err)
}
defer client.Close()
assert.NoError(t, err)

Expand Down Expand Up @@ -64,7 +67,11 @@ func TestSugaredReporting(t *testing.T) {
Tags: []string{"staging"},
}
client, err := NewClient(ctx, cfg)
if err != nil {
panic(err)
}
defer client.Close()

assert.NoError(t, err)

handlerFn := func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -78,8 +85,6 @@ func TestSugaredReporting(t *testing.T) {
ts := httptest.NewServer(client.Middleware(http.HandlerFunc(handlerFn)))
defer ts.Close()

atHTTPClient := HTTPClient(ctx)
req.SetClient(atHTTPClient)
_, err = req.Post(ts.URL+"/test",
req.Param{"param1": "abc", "param2": 123},
req.Header{
Expand Down
8 changes: 6 additions & 2 deletions sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,12 @@ func NewClient(ctx context.Context, cfg Config) (*Client, error) {

// Close cleans up the apitoolkit client. It should be called before the app shorts down, ideally as a defer call.
func (c *Client) Close() error {
c.goReqsTopic.Stop()
return c.pubsubClient.Close()
if c.goReqsTopic != nil {
c.goReqsTopic.Stop()
return c.pubsubClient.Close()

}
return nil
}

// PublishMessage publishes payload to a gcp cloud console
Expand Down
Loading

0 comments on commit 7cb2a3d

Please sign in to comment.