Skip to content

Commit

Permalink
test: expose performRequest func (#3012)
Browse files Browse the repository at this point in the history
  • Loading branch information
appleboy committed Jan 2, 2022
1 parent 0136319 commit 94153d1
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 120 deletions.
4 changes: 2 additions & 2 deletions context_test.go
Expand Up @@ -2036,8 +2036,8 @@ func TestRaceParamsContextCopy(t *testing.T) {
}(c.Copy(), c.Param("name"))
})
}
performRequest(router, "GET", "/name1/api")
performRequest(router, "GET", "/name2/api")
PerformRequest(router, "GET", "/name1/api")
PerformRequest(router, "GET", "/name2/api")
wg.Wait()
}

Expand Down
6 changes: 2 additions & 4 deletions gin_test.go
Expand Up @@ -395,7 +395,6 @@ func TestNoMethodWithoutGlobalHandlers(t *testing.T) {
}

func TestRebuild404Handlers(t *testing.T) {

}

func TestNoMethodWithGlobalHandlers(t *testing.T) {
Expand Down Expand Up @@ -491,7 +490,7 @@ func TestEngineHandleContext(t *testing.T) {
}

assert.NotPanics(t, func() {
w := performRequest(r, "GET", "/")
w := PerformRequest(r, "GET", "/")
assert.Equal(t, 301, w.Code)
})
}
Expand Down Expand Up @@ -524,7 +523,7 @@ func TestEngineHandleContextManyReEntries(t *testing.T) {
})

assert.NotPanics(t, func() {
w := performRequest(r, "GET", "/"+strconv.Itoa(expectValue-1)) // include 0 value
w := PerformRequest(r, "GET", "/"+strconv.Itoa(expectValue-1)) // include 0 value
assert.Equal(t, 200, w.Code)
assert.Equal(t, expectValue, w.Body.Len())
})
Expand Down Expand Up @@ -636,7 +635,6 @@ func TestPrepareTrustedCIRDsWith(t *testing.T) {
assert.Nil(t, r.trustedCIDRs)
assert.Nil(t, err)
}

}

func parseCIDR(cidr string) *net.IPNet {
Expand Down
10 changes: 5 additions & 5 deletions githubapi_test.go
Expand Up @@ -302,7 +302,7 @@ func TestShouldBindUri(t *testing.T) {
})

path, _ := exampleFromPath("/rest/:name/:id")
w := performRequest(router, http.MethodGet, path)
w := PerformRequest(router, http.MethodGet, path)
assert.Equal(t, "ShouldBindUri test OK", w.Body.String())
assert.Equal(t, http.StatusOK, w.Code)
}
Expand All @@ -324,7 +324,7 @@ func TestBindUri(t *testing.T) {
})

path, _ := exampleFromPath("/rest/:name/:id")
w := performRequest(router, http.MethodGet, path)
w := PerformRequest(router, http.MethodGet, path)
assert.Equal(t, "BindUri test OK", w.Body.String())
assert.Equal(t, http.StatusOK, w.Code)
}
Expand All @@ -342,7 +342,7 @@ func TestBindUriError(t *testing.T) {
})

path1, _ := exampleFromPath("/new/rest/:num")
w1 := performRequest(router, http.MethodGet, path1)
w1 := PerformRequest(router, http.MethodGet, path1)
assert.Equal(t, http.StatusBadRequest, w1.Code)
}

Expand All @@ -358,7 +358,7 @@ func TestRaceContextCopy(t *testing.T) {
go readWriteKeys(c.Copy())
c.String(http.StatusOK, "run OK, no panics")
})
w := performRequest(router, http.MethodGet, "/test/copy/race")
w := PerformRequest(router, http.MethodGet, "/test/copy/race")
assert.Equal(t, "run OK, no panics", w.Body.String())
}

Expand Down Expand Up @@ -389,7 +389,7 @@ func TestGithubAPI(t *testing.T) {

for _, route := range githubAPI {
path, values := exampleFromPath(route.path)
w := performRequest(router, route.method, path)
w := PerformRequest(router, route.method, path)

// TEST
assert.Contains(t, w.Body.String(), "\"status\":\"good\"")
Expand Down
52 changes: 25 additions & 27 deletions logger_test.go
Expand Up @@ -31,7 +31,7 @@ func TestLogger(t *testing.T) {
router.HEAD("/example", func(c *Context) {})
router.OPTIONS("/example", func(c *Context) {})

performRequest(router, "GET", "/example?a=100")
PerformRequest(router, "GET", "/example?a=100")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "GET")
assert.Contains(t, buffer.String(), "/example")
Expand All @@ -41,43 +41,43 @@ func TestLogger(t *testing.T) {
// like integration tests because they test the whole logging process rather
// than individual functions. Im not sure where these should go.
buffer.Reset()
performRequest(router, "POST", "/example")
PerformRequest(router, "POST", "/example")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "POST")
assert.Contains(t, buffer.String(), "/example")

buffer.Reset()
performRequest(router, "PUT", "/example")
PerformRequest(router, "PUT", "/example")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "PUT")
assert.Contains(t, buffer.String(), "/example")

buffer.Reset()
performRequest(router, "DELETE", "/example")
PerformRequest(router, "DELETE", "/example")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "DELETE")
assert.Contains(t, buffer.String(), "/example")

buffer.Reset()
performRequest(router, "PATCH", "/example")
PerformRequest(router, "PATCH", "/example")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "PATCH")
assert.Contains(t, buffer.String(), "/example")

buffer.Reset()
performRequest(router, "HEAD", "/example")
PerformRequest(router, "HEAD", "/example")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "HEAD")
assert.Contains(t, buffer.String(), "/example")

buffer.Reset()
performRequest(router, "OPTIONS", "/example")
PerformRequest(router, "OPTIONS", "/example")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "OPTIONS")
assert.Contains(t, buffer.String(), "/example")

buffer.Reset()
performRequest(router, "GET", "/notfound")
PerformRequest(router, "GET", "/notfound")
assert.Contains(t, buffer.String(), "404")
assert.Contains(t, buffer.String(), "GET")
assert.Contains(t, buffer.String(), "/notfound")
Expand All @@ -95,7 +95,7 @@ func TestLoggerWithConfig(t *testing.T) {
router.HEAD("/example", func(c *Context) {})
router.OPTIONS("/example", func(c *Context) {})

performRequest(router, "GET", "/example?a=100")
PerformRequest(router, "GET", "/example?a=100")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "GET")
assert.Contains(t, buffer.String(), "/example")
Expand All @@ -105,43 +105,43 @@ func TestLoggerWithConfig(t *testing.T) {
// like integration tests because they test the whole logging process rather
// than individual functions. Im not sure where these should go.
buffer.Reset()
performRequest(router, "POST", "/example")
PerformRequest(router, "POST", "/example")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "POST")
assert.Contains(t, buffer.String(), "/example")

buffer.Reset()
performRequest(router, "PUT", "/example")
PerformRequest(router, "PUT", "/example")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "PUT")
assert.Contains(t, buffer.String(), "/example")

buffer.Reset()
performRequest(router, "DELETE", "/example")
PerformRequest(router, "DELETE", "/example")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "DELETE")
assert.Contains(t, buffer.String(), "/example")

buffer.Reset()
performRequest(router, "PATCH", "/example")
PerformRequest(router, "PATCH", "/example")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "PATCH")
assert.Contains(t, buffer.String(), "/example")

buffer.Reset()
performRequest(router, "HEAD", "/example")
PerformRequest(router, "HEAD", "/example")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "HEAD")
assert.Contains(t, buffer.String(), "/example")

buffer.Reset()
performRequest(router, "OPTIONS", "/example")
PerformRequest(router, "OPTIONS", "/example")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "OPTIONS")
assert.Contains(t, buffer.String(), "/example")

buffer.Reset()
performRequest(router, "GET", "/notfound")
PerformRequest(router, "GET", "/notfound")
assert.Contains(t, buffer.String(), "404")
assert.Contains(t, buffer.String(), "GET")
assert.Contains(t, buffer.String(), "/notfound")
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestLoggerWithFormatter(t *testing.T) {
)
}))
router.GET("/example", func(c *Context) {})
performRequest(router, "GET", "/example?a=100")
PerformRequest(router, "GET", "/example?a=100")

// output test
assert.Contains(t, buffer.String(), "[FORMATTER TEST]")
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestLoggerWithConfigFormatting(t *testing.T) {
c.Request.Header.Set("X-Forwarded-For", "20.20.20.20")
gotKeys = c.Keys
})
performRequest(router, "GET", "/example?a=100")
PerformRequest(router, "GET", "/example?a=100")

// output test
assert.Contains(t, buffer.String(), "[FORMATTER TEST]")
Expand All @@ -228,7 +228,6 @@ func TestLoggerWithConfigFormatting(t *testing.T) {
assert.Equal(t, "/example?a=100", gotParam.Path)
assert.Empty(t, gotParam.ErrorMessage)
assert.Equal(t, gotKeys, gotParam.Keys)

}

func TestDefaultLogFormatter(t *testing.T) {
Expand Down Expand Up @@ -282,7 +281,6 @@ func TestDefaultLogFormatter(t *testing.T) {

assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 |\x1b[97;42m 200 \x1b[0m| 5s | 20.20.20.20 |\x1b[97;44m GET \x1b[0m \"/\"\n", defaultLogFormatter(termTrueParam))
assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 |\x1b[97;42m 200 \x1b[0m| 2743h29m3s | 20.20.20.20 |\x1b[97;44m GET \x1b[0m \"/\"\n", defaultLogFormatter(termTrueLongDurationParam))

}

func TestColorForMethod(t *testing.T) {
Expand Down Expand Up @@ -369,15 +367,15 @@ func TestErrorLogger(t *testing.T) {
c.String(http.StatusInternalServerError, "hola!")
})

w := performRequest(router, "GET", "/error")
w := PerformRequest(router, "GET", "/error")
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "{\"error\":\"this is an error\"}", w.Body.String())

w = performRequest(router, "GET", "/abort")
w = PerformRequest(router, "GET", "/abort")
assert.Equal(t, http.StatusUnauthorized, w.Code)
assert.Equal(t, "{\"error\":\"no authorized\"}", w.Body.String())

w = performRequest(router, "GET", "/print")
w = PerformRequest(router, "GET", "/print")
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Equal(t, "hola!{\"error\":\"this is an error\"}", w.Body.String())
}
Expand All @@ -389,11 +387,11 @@ func TestLoggerWithWriterSkippingPaths(t *testing.T) {
router.GET("/logged", func(c *Context) {})
router.GET("/skipped", func(c *Context) {})

performRequest(router, "GET", "/logged")
PerformRequest(router, "GET", "/logged")
assert.Contains(t, buffer.String(), "200")

buffer.Reset()
performRequest(router, "GET", "/skipped")
PerformRequest(router, "GET", "/skipped")
assert.Contains(t, buffer.String(), "")
}

Expand All @@ -407,11 +405,11 @@ func TestLoggerWithConfigSkippingPaths(t *testing.T) {
router.GET("/logged", func(c *Context) {})
router.GET("/skipped", func(c *Context) {})

performRequest(router, "GET", "/logged")
PerformRequest(router, "GET", "/logged")
assert.Contains(t, buffer.String(), "200")

buffer.Reset()
performRequest(router, "GET", "/skipped")
PerformRequest(router, "GET", "/skipped")
assert.Contains(t, buffer.String(), "")
}

Expand Down
17 changes: 8 additions & 9 deletions middleware_test.go
Expand Up @@ -35,7 +35,7 @@ func TestMiddlewareGeneralCase(t *testing.T) {
signature += " XX "
})
// RUN
w := performRequest(router, "GET", "/")
w := PerformRequest(router, "GET", "/")

// TEST
assert.Equal(t, http.StatusOK, w.Code)
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestMiddlewareNoRoute(t *testing.T) {
signature += " X "
})
// RUN
w := performRequest(router, "GET", "/")
w := PerformRequest(router, "GET", "/")

// TEST
assert.Equal(t, http.StatusNotFound, w.Code)
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestMiddlewareNoMethodEnabled(t *testing.T) {
signature += " XX "
})
// RUN
w := performRequest(router, "GET", "/")
w := PerformRequest(router, "GET", "/")

// TEST
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestMiddlewareNoMethodDisabled(t *testing.T) {
})

// RUN
w := performRequest(router, "GET", "/")
w := PerformRequest(router, "GET", "/")

// TEST
assert.Equal(t, http.StatusNotFound, w.Code)
Expand All @@ -175,7 +175,7 @@ func TestMiddlewareAbort(t *testing.T) {
})

// RUN
w := performRequest(router, "GET", "/")
w := PerformRequest(router, "GET", "/")

// TEST
assert.Equal(t, http.StatusUnauthorized, w.Code)
Expand All @@ -190,14 +190,13 @@ func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
c.Next()
c.AbortWithStatus(http.StatusGone)
signature += "B"

})
router.GET("/", func(c *Context) {
signature += "C"
c.Next()
})
// RUN
w := performRequest(router, "GET", "/")
w := PerformRequest(router, "GET", "/")

// TEST
assert.Equal(t, http.StatusGone, w.Code)
Expand All @@ -220,7 +219,7 @@ func TestMiddlewareFailHandlersChain(t *testing.T) {
signature += "C"
})
// RUN
w := performRequest(router, "GET", "/")
w := PerformRequest(router, "GET", "/")

// TEST
assert.Equal(t, http.StatusInternalServerError, w.Code)
Expand All @@ -247,7 +246,7 @@ func TestMiddlewareWrite(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := PerformRequest(router, "GET", "/")

assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Equal(t, strings.Replace("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}{\"foo\":\"bar\"}event:test\ndata:message\n\n", " ", "", -1), strings.Replace(w.Body.String(), " ", "", -1))
Expand Down

0 comments on commit 94153d1

Please sign in to comment.