Skip to content

Commit

Permalink
hotfix: testes
Browse files Browse the repository at this point in the history
  • Loading branch information
viquitorreis committed Apr 15, 2024
1 parent a37c4f8 commit b6ba2e8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
44 changes: 22 additions & 22 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func MiddlewareSample(next APIFunc) APIFunc {

return func(tc *TupaContext) error {
smpMidd := "sampleMiddleware"
reqCtx := context.WithValue(tc.request.Context(), "smpMidd", smpMidd)
tc.request = tc.request.WithContext(reqCtx)
reqCtx := context.WithValue(tc.Req.Context(), "smpMidd", smpMidd)
tc.Req = tc.Req.WithContext(reqCtx)

log.SetFlags(log.LstdFlags | log.Lmicroseconds)
fmt.Println("Middleware antes de chamar o handler")
Expand All @@ -29,7 +29,7 @@ func MiddlewareSample(next APIFunc) APIFunc {
}

func getCtxFromSampleMiddleware(tc *TupaContext) {
ctxValue := tc.request.Context().Value("smpMidd").(string)
ctxValue := tc.Req.Context().Value("smpMidd").(string)

fmt.Println(ctxValue)
}
Expand All @@ -39,15 +39,15 @@ func MiddlewareLoggingWithError(next APIFunc) APIFunc {

start := time.Now()
errMsg := errors.New("erro no middleware LoggingMiddlewareWithError")
ctx := context.WithValue(tc.request.Context(), "smpErrorMidd", "sampleErrorMiddleware")
tc.request = tc.request.WithContext(ctx)
ctx := context.WithValue(tc.Req.Context(), "smpErrorMidd", "sampleErrorMiddleware")
tc.Req = tc.Req.WithContext(ctx)

err := next(tc)
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
log.Printf("Fim da request para endpoint teste: %s, duração: %v", tc.request.URL.Path, time.Since(start))
log.Printf("Fim da Req para endpoint teste: %s, duração: %v", tc.Req.URL.Path, time.Since(start))

if err != nil {
log.Printf("erro ao chamar proximo middleware %s: %v", tc.request.URL.Path, err)
log.Printf("erro ao chamar proximo middleware %s: %v", tc.Req.URL.Path, err)
}

return errMsg
Expand All @@ -58,8 +58,8 @@ func MiddlewareWithCtx(next APIFunc) APIFunc {

return func(tc *TupaContext) error {
smpMidd := "MiddlewareWithCtx"
reqCtx := context.WithValue(tc.request.Context(), "withCtx", smpMidd)
tc.request = tc.request.WithContext(reqCtx)
reqCtx := context.WithValue(tc.Req.Context(), "withCtx", smpMidd)
tc.Req = tc.Req.WithContext(reqCtx)

log.SetFlags(log.LstdFlags | log.Lmicroseconds)

Expand All @@ -71,8 +71,8 @@ func MiddlewareWithCtxChanMsg(next APIFunc, messages chan<- string) APIFunc {

return func(tc *TupaContext) error {
smpMidd := "MiddlewareWithCtx"
reqCtx := context.WithValue(tc.request.Context(), "withCtx", smpMidd)
tc.request = tc.request.WithContext(reqCtx)
reqCtx := context.WithValue(tc.Req.Context(), "withCtx", smpMidd)
tc.Req = tc.Req.WithContext(reqCtx)

log.SetFlags(log.LstdFlags | log.Lmicroseconds)

Expand All @@ -88,14 +88,14 @@ func TestSampleMiddleware(t *testing.T) {
w := httptest.NewRecorder()

ctx := &TupaContext{
request: req,
response: w,
Req: req,
Resp: w,
}

// handler que não retorna erro
handler := func(tc *TupaContext) error {
// Chacando se o middleware tem valor de context
if val := tc.request.Context().Value("qualquerKey"); val != nil {
if val := tc.Req.Context().Value("qualquerKey"); val != nil {
t.Error("Valor de context esperado não era esperado")
}
return nil
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestMiddlewareConcurrency(t *testing.T) {
middleware := MiddlewareWithCtx

handler := func(tc *TupaContext) error {
ctxValue := tc.request.Context().Value("withCtx").(string)
ctxValue := tc.Req.Context().Value("withCtx").(string)

if ctxValue != "MiddlewareWithCtx" {
t.Errorf("Esperava valor de context 'MiddlewareWithCtx', recebeu '%s'", ctxValue)
Expand All @@ -139,8 +139,8 @@ func TestMiddlewareConcurrency(t *testing.T) {
w := httptest.NewRecorder()

ctx := &TupaContext{
request: req,
response: w,
Req: req,
Resp: w,
}

// Chanmando o middleware com o handler e o contexto
Expand All @@ -167,7 +167,7 @@ func TestMiddlewareWithCtxAndChannels(t *testing.T) {
middleware := MiddlewareWithCtx

handler := func(tc *TupaContext) error {
ctxValue := tc.request.Context().Value("withCtx").(string)
ctxValue := tc.Req.Context().Value("withCtx").(string)

ctxValues <- ctxValue

Expand All @@ -180,8 +180,8 @@ func TestMiddlewareWithCtxAndChannels(t *testing.T) {
w := httptest.NewRecorder()

ctx := &TupaContext{
request: req,
response: w,
Req: req,
Resp: w,
}

err := middleware(handler)(ctx)
Expand Down Expand Up @@ -225,8 +225,8 @@ func TestMiddlewareWithCtxAndChannelAndMsg(t *testing.T) {
w := httptest.NewRecorder()

ctx := &TupaContext{
request: req,
response: w,
Req: req,
Resp: w,
}

err := middleware(handler)(ctx)
Expand Down
16 changes: 8 additions & 8 deletions tupa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func BenchmarkDirectAccessSendString(b *testing.B) {
req, _ := http.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
ctx := &TupaContext{
request: req,
response: w,
Req: req,
Resp: w,
}

start := time.Now()
Expand Down Expand Up @@ -43,7 +43,7 @@ func TestParam(t *testing.T) {
})

tc := &TupaContext{
request: req,
Req: req,
}

got := tc.Param("id")
Expand All @@ -60,7 +60,7 @@ func TestParam(t *testing.T) {
}

tc := &TupaContext{
request: req,
Req: req,
}

got := tc.Param("id")
Expand All @@ -79,7 +79,7 @@ func TestQueryParam(t *testing.T) {
}

tc := &TupaContext{
request: req,
Req: req,
}

got := tc.QueryParam("name")
Expand All @@ -96,7 +96,7 @@ func TestQueryParam(t *testing.T) {
}

tc := &TupaContext{
request: req,
Req: req,
}

got := tc.QueryParam("name")
Expand All @@ -115,7 +115,7 @@ func TestQueryParams(t *testing.T) {
}

tc := &TupaContext{
request: req,
Req: req,
}

got := tc.QueryParams()
Expand All @@ -135,7 +135,7 @@ func TestQueryParams(t *testing.T) {
}

tc := &TupaContext{
request: req,
Req: req,
}

got := tc.QueryParams()
Expand Down

0 comments on commit b6ba2e8

Please sign in to comment.