forked from mirror/gin
Compare commits
1 Commits
master
...
benchmarks
Author | SHA1 | Date |
---|---|---|
Manu Mtz-Almeida | 1abc373aff |
|
@ -1,49 +0,0 @@
|
|||
- With issues:
|
||||
- Use the search tool before opening a new issue.
|
||||
- Please provide source code and commit sha if you found a bug.
|
||||
- Review existing issues and provide feedback or react to them.
|
||||
|
||||
## Description
|
||||
|
||||
<!-- Description of a problem -->
|
||||
|
||||
## How to reproduce
|
||||
|
||||
<!-- The smallest possible code example to show the problem that can be compiled, like -->
|
||||
```
|
||||
package main
|
||||
|
||||
import (
|
||||
"git.internal/re/gin"
|
||||
)
|
||||
|
||||
func main() {
|
||||
g := gin.Default()
|
||||
g.GET("/hello/:name", func(c *gin.Context) {
|
||||
c.String(200, "Hello %s", c.Param("name"))
|
||||
})
|
||||
g.Run(":9000")
|
||||
}
|
||||
```
|
||||
|
||||
## Expectations
|
||||
|
||||
<!-- Your expectation result of 'curl' command, like -->
|
||||
```
|
||||
$ curl http://localhost:8201/hello/world
|
||||
Hello world
|
||||
```
|
||||
|
||||
## Actual result
|
||||
|
||||
<!-- Actual result showing the problem -->
|
||||
```
|
||||
$ curl -i http://localhost:8201/hello/world
|
||||
<YOUR RESULT>
|
||||
```
|
||||
|
||||
## Environment
|
||||
|
||||
- go version:
|
||||
- gin version (or commit ref):
|
||||
- operating system:
|
|
@ -1,7 +0,0 @@
|
|||
- With pull requests:
|
||||
- Open your pull request against `master`
|
||||
- Your pull request should have no more than two commits, if not you should squash them.
|
||||
- It should pass all tests in the available continuous integration systems such as GitHub Actions.
|
||||
- You should add/modify tests to cover your proposed code changes.
|
||||
- If your pull request contains a new feature, please document it on the README.
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: github-actions
|
||||
directory: /
|
||||
schedule:
|
||||
interval: weekly
|
||||
- package-ecosystem: gomod
|
||||
directory: /
|
||||
schedule:
|
||||
interval: weekly
|
|
@ -1,49 +0,0 @@
|
|||
# For most projects, this workflow file will not need changing; you simply need
|
||||
# to commit it to your repository.
|
||||
#
|
||||
# You may wish to alter this file to override the set of languages analyzed,
|
||||
# or to provide custom queries or build logic.
|
||||
name: "CodeQL"
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
# The branches below must be a subset of the branches above
|
||||
branches: [ master ]
|
||||
schedule:
|
||||
- cron: '0 17 * * 5'
|
||||
|
||||
jobs:
|
||||
analyze:
|
||||
name: Analyze
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
permissions:
|
||||
# required for all workflows
|
||||
security-events: write
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
# Override automatic language detection by changing the below list
|
||||
# Supported options are ['csharp', 'cpp', 'go', 'java', 'javascript', 'python']
|
||||
# TODO: Enable for javascript later
|
||||
language: [ 'go']
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v2
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
# By default, queries listed here will override any specified in a config file.
|
||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v2
|
|
@ -1,91 +0,0 @@
|
|||
name: Run Tests
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Setup go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: '^1.16'
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
- name: Setup golangci-lint
|
||||
uses: golangci/golangci-lint-action@v3.3.1
|
||||
with:
|
||||
version: v1.48.0
|
||||
args: --verbose
|
||||
test:
|
||||
needs: lint
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, macos-latest]
|
||||
go: [1.16, 1.17, 1.18, 1.19]
|
||||
test-tags: ['', '-tags nomsgpack', '-tags "sonic avx"', '-tags go_json']
|
||||
include:
|
||||
- os: ubuntu-latest
|
||||
go-build: ~/.cache/go-build
|
||||
- os: macos-latest
|
||||
go-build: ~/Library/Caches/go-build
|
||||
name: ${{ matrix.os }} @ Go ${{ matrix.go }} ${{ matrix.test-tags }}
|
||||
runs-on: ${{ matrix.os }}
|
||||
env:
|
||||
GO111MODULE: on
|
||||
TESTTAGS: ${{ matrix.test-tags }}
|
||||
GOPROXY: https://proxy.golang.org
|
||||
steps:
|
||||
- name: Set up Go ${{ matrix.go }}
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: ${{ matrix.go }}
|
||||
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
ref: ${{ github.ref }}
|
||||
|
||||
- uses: actions/cache@v3
|
||||
with:
|
||||
path: |
|
||||
${{ matrix.go-build }}
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-
|
||||
|
||||
- name: Run Tests
|
||||
run: make test
|
||||
|
||||
- name: Upload coverage to Codecov
|
||||
uses: codecov/codecov-action@v3
|
||||
with:
|
||||
flags: ${{ matrix.os }},go-${{ matrix.go }},${{ matrix.test-tags }}
|
||||
|
||||
- name: Format
|
||||
if: matrix.go-version == '1.19.x'
|
||||
run: diff -u <(echo -n) <(gofmt -d .)
|
||||
notification-gitter:
|
||||
needs: test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Notification failure message
|
||||
if: failure()
|
||||
run: |
|
||||
PR_OR_COMPARE="$(if [ "${{ github.event.pull_request }}" != "" ]; then echo "${{ github.event.pull_request.html_url }}"; else echo "${{ github.event.compare }}"; fi)"
|
||||
curl -d message="GitHub Actions [$GITHUB_REPOSITORY]($PR_OR_COMPARE) ($GITHUB_REF) [normal]($GITHUB_API_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID) ($GITHUB_RUN_NUMBER)" -d level=error https://webhooks.gitter.im/e/7f95bf605c4d356372f4
|
||||
- name: Notification success message
|
||||
if: success()
|
||||
run: |
|
||||
PR_OR_COMPARE="$(if [ "${{ github.event.pull_request }}" != "" ]; then echo "${{ github.event.pull_request.html_url }}"; else echo "${{ github.event.compare }}"; fi)"
|
||||
curl -d message="GitHub Actions [$GITHUB_REPOSITORY]($PR_OR_COMPARE) ($GITHUB_REF) [normal]($GITHUB_API_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID) ($GITHUB_RUN_NUMBER)" https://webhooks.gitter.im/e/7f95bf605c4d356372f4
|
|
@ -1,34 +0,0 @@
|
|||
name: Goreleaser
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- '*'
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
goreleaser:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
-
|
||||
name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
-
|
||||
name: Set up Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.17
|
||||
-
|
||||
name: Run GoReleaser
|
||||
uses: goreleaser/goreleaser-action@v4
|
||||
with:
|
||||
# either 'goreleaser' (default) or 'goreleaser-pro'
|
||||
distribution: goreleaser
|
||||
version: latest
|
||||
args: release --rm-dist
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
@ -1,7 +0,0 @@
|
|||
vendor/*
|
||||
!vendor/vendor.json
|
||||
coverage.out
|
||||
count.out
|
||||
test
|
||||
profile.out
|
||||
tmp.out
|
|
@ -1,39 +0,0 @@
|
|||
run:
|
||||
timeout: 5m
|
||||
linters:
|
||||
enable:
|
||||
- asciicheck
|
||||
- depguard
|
||||
- dogsled
|
||||
- durationcheck
|
||||
- errcheck
|
||||
- errorlint
|
||||
- exportloopref
|
||||
- gci
|
||||
- gofmt
|
||||
- goimports
|
||||
- gosec
|
||||
- misspell
|
||||
- nakedret
|
||||
- nilerr
|
||||
- nolintlint
|
||||
- revive
|
||||
- wastedassign
|
||||
issues:
|
||||
exclude-rules:
|
||||
- linters:
|
||||
- structcheck
|
||||
- unused
|
||||
text: "`data` is unused"
|
||||
- linters:
|
||||
- staticcheck
|
||||
text: "SA1019:"
|
||||
- linters:
|
||||
- revive
|
||||
text: "var-naming:"
|
||||
- linters:
|
||||
- revive
|
||||
text: "exported:"
|
||||
- path: _test\.go
|
||||
linters:
|
||||
- gosec # security is not make sense in tests
|
|
@ -1,57 +0,0 @@
|
|||
project_name: gin
|
||||
|
||||
builds:
|
||||
-
|
||||
# If true, skip the build.
|
||||
# Useful for library projects.
|
||||
# Default is false
|
||||
skip: true
|
||||
|
||||
changelog:
|
||||
# Set it to true if you wish to skip the changelog generation.
|
||||
# This may result in an empty release notes on GitHub/GitLab/Gitea.
|
||||
skip: false
|
||||
|
||||
# Changelog generation implementation to use.
|
||||
#
|
||||
# Valid options are:
|
||||
# - `git`: uses `git log`;
|
||||
# - `github`: uses the compare GitHub API, appending the author login to the changelog.
|
||||
# - `gitlab`: uses the compare GitLab API, appending the author name and email to the changelog.
|
||||
# - `github-native`: uses the GitHub release notes generation API, disables the groups feature.
|
||||
#
|
||||
# Defaults to `git`.
|
||||
use: git
|
||||
|
||||
# Sorts the changelog by the commit's messages.
|
||||
# Could either be asc, desc or empty
|
||||
# Default is empty
|
||||
sort: asc
|
||||
|
||||
# Group commits messages by given regex and title.
|
||||
# Order value defines the order of the groups.
|
||||
# Proving no regex means all commits will be grouped under the default group.
|
||||
# Groups are disabled when using github-native, as it already groups things by itself.
|
||||
#
|
||||
# Default is no groups.
|
||||
groups:
|
||||
- title: Features
|
||||
regexp: "^.*feat[(\\w)]*:+.*$"
|
||||
order: 0
|
||||
- title: 'Bug fixes'
|
||||
regexp: "^.*fix[(\\w)]*:+.*$"
|
||||
order: 1
|
||||
- title: 'Enhancements'
|
||||
regexp: "^.*chore[(\\w)]*:+.*$"
|
||||
order: 2
|
||||
- title: Others
|
||||
order: 999
|
||||
|
||||
filters:
|
||||
# Commit messages matching the regexp listed here will be removed from
|
||||
# the changelog
|
||||
# Default is empty
|
||||
exclude:
|
||||
- '^docs'
|
||||
- 'CICD'
|
||||
- typo
|
|
@ -0,0 +1,7 @@
|
|||
language: go
|
||||
|
||||
go:
|
||||
- 1.1
|
||||
- 1.2
|
||||
- 1.3
|
||||
- tip
|
406
AUTHORS.md
406
AUTHORS.md
|
@ -1,406 +0,0 @@
|
|||
List of all the awesome people working to make Gin the best Web Framework in Go.
|
||||
|
||||
## gin 1.x series authors
|
||||
|
||||
**Gin Core Team:** Bo-Yi Wu (@appleboy), thinkerou (@thinkerou), Javier Provecho (@javierprovecho)
|
||||
|
||||
## gin 0.x series authors
|
||||
|
||||
**Maintainers:** Manu Martinez-Almeida (@manucorporat), Javier Provecho (@javierprovecho)
|
||||
|
||||
------
|
||||
|
||||
People and companies, who have contributed, in alphabetical order.
|
||||
|
||||
- 178inaba <178inaba@users.noreply.github.com>
|
||||
- A. F <hello@clivern.com>
|
||||
- ABHISHEK SONI <abhishek.rocks26@gmail.com>
|
||||
- Abhishek Chanda <achanda@users.noreply.github.com>
|
||||
- Abner Chen <houjunchen@gmail.com>
|
||||
- AcoNCodes <acongame@gmail.com>
|
||||
- Adam Dratwinski <adam.dratwinski@gmail.com>
|
||||
- Adam Mckaig <adam.mckaig@gmail.com>
|
||||
- Adam Zielinski <MusicAdam@users.noreply.github.com>
|
||||
- Adonis <donileo@gmail.com>
|
||||
- Alan Wang <azzwacb9001@126.com>
|
||||
- Albin Gilles <gilles.albin@gmail.com>
|
||||
- Aleksandr Didenko <aa.didenko@yandex.ru>
|
||||
- Alessandro (Ale) Segala <43508+ItalyPaleAle@users.noreply.github.com>
|
||||
- Alex <AWulkan@users.noreply.github.com>
|
||||
- Alexander <alexanderchenmh@gmail.com>
|
||||
- Alexander Lokhman <alex.lokhman@gmail.com>
|
||||
- Alexander Melentyev <55826637+alexander-melentyev@users.noreply.github.com>
|
||||
- Alexander Nyquist <nyquist.alexander@gmail.com>
|
||||
- Allen Ren <kulong0105@gmail.com>
|
||||
- AllinGo <tanhp@outlook.com>
|
||||
- Ammar Bandukwala <ammar@ammar.io>
|
||||
- An Xiao (Luffy) <hac@zju.edu.cn>
|
||||
- Andre Dublin <81dublin@gmail.com>
|
||||
- Andrew Szeto <github@jabagawee.com>
|
||||
- Andrey Abramov <andreyabramov.aaa@gmail.com>
|
||||
- Andrey Nering <andrey.nering@gmail.com>
|
||||
- Andrey Smirnov <Smirnov.Andrey@gmail.com>
|
||||
- Andrii Bubis <firstrow@gmail.com>
|
||||
- André Bazaglia <bazaglia@users.noreply.github.com>
|
||||
- Andy Pan <panjf2000@gmail.com>
|
||||
- Antoine GIRARD <sapk@users.noreply.github.com>
|
||||
- Anup Kumar Panwar <1anuppanwar@gmail.com>
|
||||
- Aravinth Sundaram <gosh.aravind@gmail.com>
|
||||
- Artem <horechek@gmail.com>
|
||||
- Ashwani <ashwanisharma686@gmail.com>
|
||||
- Aurelien Regat-Barrel <arb@cyberkarma.net>
|
||||
- Austin Heap <me@austinheap.com>
|
||||
- Barnabus <jbampton@users.noreply.github.com>
|
||||
- Bo-Yi Wu <appleboy.tw@gmail.com>
|
||||
- Boris Borshevsky <BorisBorshevsky@gmail.com>
|
||||
- Boyi Wu <p581581@gmail.com>
|
||||
- BradyBromley <51128276+BradyBromley@users.noreply.github.com>
|
||||
- Brendan Fosberry <brendan@shopkeep.com>
|
||||
- Brian Wigginton <brianwigginton@gmail.com>
|
||||
- Carlos Eduardo <carlosedp@gmail.com>
|
||||
- Chad Russell <chaddouglasrussell@gmail.com>
|
||||
- Charles <cxjava@gmail.com>
|
||||
- Christian Muehlhaeuser <muesli@gmail.com>
|
||||
- Christian Persson <saser@live.se>
|
||||
- Christopher Harrington <ironiridis@gmail.com>
|
||||
- Damon Zhao <yijun.zhao@outlook.com>
|
||||
- Dan Markham <dmarkham@gmail.com>
|
||||
- Dang Nguyen <hoangdang.me@gmail.com>
|
||||
- Daniel Krom <kromdan@gmail.com>
|
||||
- Daniel M. Lambea <dmlambea@gmail.com>
|
||||
- Danieliu <liudanking@gmail.com>
|
||||
- David Irvine <aviddiviner@gmail.com>
|
||||
- David Zhang <crispgm@gmail.com>
|
||||
- Davor Kapsa <dvrkps@users.noreply.github.com>
|
||||
- DeathKing <DeathKing@users.noreply.github.com>
|
||||
- Dennis Cho <47404603+forest747@users.noreply.github.com>
|
||||
- Dmitry Dorogin <dmirogin@ya.ru>
|
||||
- Dmitry Kutakov <vkd.castle@gmail.com>
|
||||
- Dmitry Sedykh <dmitrys@d3h.local>
|
||||
- Don2Quixote <35610661+Don2Quixote@users.noreply.github.com>
|
||||
- Donn Pebe <iam@donnpebe.com>
|
||||
- Dustin Decker <dustindecker@protonmail.com>
|
||||
- Eason Lin <easonlin404@gmail.com>
|
||||
- Edward Betts <edward@4angle.com>
|
||||
- Egor Seredin <4819888+agmt@users.noreply.github.com>
|
||||
- Emmanuel Goh <emmanuel@visenze.com>
|
||||
- Equim <sayaka@ekyu.moe>
|
||||
- Eren A. Akyol <eren@redmc.me>
|
||||
- Eric_Lee <xplzv@126.com>
|
||||
- Erik Bender <erik.bender@develerik.dev>
|
||||
- Ethan Kan <ethankan@neoplot.com>
|
||||
- Evgeny Persienko <e.persienko@office.ngs.ru>
|
||||
- Faisal Alam <ifaisalalam@gmail.com>
|
||||
- Fareed Dudhia <fareeddudhia@googlemail.com>
|
||||
- Filip Figiel <figiel.filip@gmail.com>
|
||||
- Florian Polster <couchpolster@icqmail.com>
|
||||
- Frank Bille <github@frankbille.dk>
|
||||
- Franz Bettag <franz@bett.ag>
|
||||
- Ganlv <ganlvtech@users.noreply.github.com>
|
||||
- Gaozhen Ying <yinggaozhen@hotmail.com>
|
||||
- George Gabolaev <gabolaev98@gmail.com>
|
||||
- George Kirilenko <necryin@users.noreply.github.com>
|
||||
- Georges Varouchas <georges.varouchas@gmail.com>
|
||||
- Gordon Tyler <gordon@doxxx.net>
|
||||
- Harindu Perera <harinduenator@gmail.com>
|
||||
- Helios <674876158@qq.com>
|
||||
- Henry Kwan <piengeng@users.noreply.github.com>
|
||||
- Henry Yee <henry@yearning.io>
|
||||
- Himanshu Mishra <OrkoHunter@users.noreply.github.com>
|
||||
- Hiroyuki Tanaka <h.tanaka.0325@gmail.com>
|
||||
- Ibraheem Ahmed <ibrah1440@gmail.com>
|
||||
- Ignacio Galindo <joiggama@gmail.com>
|
||||
- Igor H. Vieira <zignd.igor@gmail.com>
|
||||
- Ildar1111 <54001462+Ildar1111@users.noreply.github.com>
|
||||
- Iskander (Alex) Sharipov <iskander.sharipov@intel.com>
|
||||
- Ismail Gjevori <isgjevori@protonmail.com>
|
||||
- Ivan Chen <allenivan@gmail.com>
|
||||
- JINNOUCHI Yasushi <delphinus@remora.cx>
|
||||
- James Pettyjohn <japettyjohn@users.noreply.github.com>
|
||||
- Jamie Stackhouse <jamie.stackhouse@redspace.com>
|
||||
- Jason Lee <jawc@hotmail.com>
|
||||
- Javier Provecho <j.provecho@dartekstudios.com>
|
||||
- Javier Provecho <javier.provecho@bq.com>
|
||||
- Javier Provecho <javiertitan@gmail.com>
|
||||
- Javier Provecho Fernandez <j.provecho@dartekstudios.com>
|
||||
- Javier Provecho Fernandez <javiertitan@gmail.com>
|
||||
- Jean-Christophe Lebreton <jclebreton@gmail.com>
|
||||
- Jeff <laojianzi1994@gmail.com>
|
||||
- Jeremy Loy <jeremy.b.loy@icloud.com>
|
||||
- Jim Filippou <p3160253@aueb.gr>
|
||||
- Jimmy Pettersson <jimmy@expertmaker.com>
|
||||
- John Bampton <jbampton@users.noreply.github.com>
|
||||
- Johnny Dallas <johnnydallas0308@gmail.com>
|
||||
- Johnny Dallas <theonlyjohnny@theonlyjohnny.sh>
|
||||
- Jonathan (JC) Chen <jc@dijonkitchen.org>
|
||||
- Josep Jesus Bigorra Algaba <42377845+averageflow@users.noreply.github.com>
|
||||
- Josh Horowitz <joshua.m.horowitz@gmail.com>
|
||||
- Joshua Loper <josh.el3@gmail.com>
|
||||
- Julien Schmidt <github@julienschmidt.com>
|
||||
- Jun Kimura <jksmphone@gmail.com>
|
||||
- Justin Beckwith <justin.beckwith@gmail.com>
|
||||
- Justin Israel <justinisrael@gmail.com>
|
||||
- Justin Mayhew <mayhew@live.ca>
|
||||
- Jérôme Laforge <jerome-laforge@users.noreply.github.com>
|
||||
- Kacper Bąk <56700396+53jk1@users.noreply.github.com>
|
||||
- Kamron Batman <kamronbatman@users.noreply.github.com>
|
||||
- Kane Rogers <kane@cleanstream.com.au>
|
||||
- Kaushik Neelichetty <kaushikneelichetty6132@gmail.com>
|
||||
- Keiji Yoshida <yoshida.keiji.84@gmail.com>
|
||||
- Kel Cecil <kel.cecil@listhub.com>
|
||||
- Kevin Mulvey <kmulvey@linux.com>
|
||||
- Kevin Zhu <ipandtcp@gmail.com>
|
||||
- Kirill Motkov <motkov.kirill@gmail.com>
|
||||
- Klemen Sever <ksever@student.42.fr>
|
||||
- Kristoffer A. Iversen <kristoffer.a.iversen@gmail.com>
|
||||
- Krzysztof Szafrański <k.p.szafranski@gmail.com>
|
||||
- Kumar McMillan <kumar.mcmillan@gmail.com>
|
||||
- Kyle Mcgill <email@kylescottmcgill.com>
|
||||
- Lanco <35420416+lancoLiu@users.noreply.github.com>
|
||||
- Levi Olson <olson.levi@gmail.com>
|
||||
- Lin Kao-Yuan <mosdeo@gmail.com>
|
||||
- Linus Unnebäck <linus@folkdatorn.se>
|
||||
- Lucas Clemente <lucas@clemente.io>
|
||||
- Ludwig Valda Vasquez <bredov@gmail.com>
|
||||
- Luis GG <lggomez@users.noreply.github.com>
|
||||
- MW Lim <williamchange@gmail.com>
|
||||
- Maksimov Sergey <konjoot@gmail.com>
|
||||
- Manjusaka <lizheao940510@gmail.com>
|
||||
- Manu MA <manu.mtza@gmail.com>
|
||||
- Manu MA <manu.valladolid@gmail.com>
|
||||
- Manu Mtz-Almeida <manu.valladolid@gmail.com>
|
||||
- Manu Mtz.-Almeida <manu.valladolid@gmail.com>
|
||||
- Manuel Alonso <manuelalonso@invisionapp.com>
|
||||
- Mara Kim <hacker.root@gmail.com>
|
||||
- Mario Kostelac <mario@intercom.io>
|
||||
- Martin Karlsch <martin@karlsch.com>
|
||||
- Matt Newberry <mnewberry@opentable.com>
|
||||
- Matt Williams <gh@mattyw.net>
|
||||
- Matthieu MOREL <mmorel-35@users.noreply.github.com>
|
||||
- Max Hilbrunner <mhilbrunner@users.noreply.github.com>
|
||||
- Maxime Soulé <btik-git@scoubidou.com>
|
||||
- MetalBreaker <johnymichelson@gmail.com>
|
||||
- Michael Puncel <mpuncel@squareup.com>
|
||||
- MichaelDeSteven <51652084+MichaelDeSteven@users.noreply.github.com>
|
||||
- Mike <38686456+icy4ever@users.noreply.github.com>
|
||||
- Mike Stipicevic <mst@ableton.com>
|
||||
- Miki Tebeka <miki.tebeka@gmail.com>
|
||||
- Miles <MilesLin@users.noreply.github.com>
|
||||
- Mirza Ceric <mirza.ceric@b2match.com>
|
||||
- Mykyta Semenistyi <nikeiwe@gmail.com>
|
||||
- Naoki Takano <honten@tinkermode.com>
|
||||
- Ngalim Siregar <ngalim.siregar@gmail.com>
|
||||
- Ni Hao <supernihaooo@qq.com>
|
||||
- Nick Gerakines <nick@gerakines.net>
|
||||
- Nikifor Seryakov <nikandfor@gmail.com>
|
||||
- Notealot <714804968@qq.com>
|
||||
- Olivier Mengué <dolmen@cpan.org>
|
||||
- Olivier Robardet <orobardet@users.noreply.github.com>
|
||||
- Pablo Moncada <pablo.moncada@bq.com>
|
||||
- Pablo Moncada <pmoncadaisla@gmail.com>
|
||||
- Panmax <967168@qq.com>
|
||||
- Peperoncino <2wua4nlyi@gmail.com>
|
||||
- Philipp Meinen <philipp@bind.ch>
|
||||
- Pierre Massat <pierre@massat.io>
|
||||
- Qt <golang.chen@gmail.com>
|
||||
- Quentin ROYER <aydendevg@gmail.com>
|
||||
- README Bot <35302948+codetriage-readme-bot@users.noreply.github.com>
|
||||
- Rafal Zajac <rzajac@gmail.com>
|
||||
- Rahul Datta Roy <rahuldroy@users.noreply.github.com>
|
||||
- Rajiv Kilaparti <rajivk085@gmail.com>
|
||||
- Raphael Gavache <raphael.gavache@datadoghq.com>
|
||||
- Ray Rodriguez <rayrod2030@gmail.com>
|
||||
- Regner Blok-Andersen <shadowdf@gmail.com>
|
||||
- Remco <remco@dutchcoders.io>
|
||||
- Rex Lee(李俊) <duguying2008@gmail.com>
|
||||
- Richard Lee <dlackty@gmail.com>
|
||||
- Riverside <wangyb65@gmail.com>
|
||||
- Robert Wilkinson <wilkinson.robert.a@gmail.com>
|
||||
- Rogier Lommers <rogier@lommers.org>
|
||||
- Rohan Pai <me@rohanpai.com>
|
||||
- Romain Beuque <rbeuque74@gmail.com>
|
||||
- Roman Belyakovsky <ihryamzik@gmail.com>
|
||||
- Roman Zaynetdinov <627197+zaynetro@users.noreply.github.com>
|
||||
- Roman Zaynetdinov <roman.zaynetdinov@lekane.com>
|
||||
- Ronald Petty <ronald.petty@rx-m.com>
|
||||
- Ross Wolf <31489089+rw-access@users.noreply.github.com>
|
||||
- Roy Lou <roylou@gmail.com>
|
||||
- Rubi <14269809+codenoid@users.noreply.github.com>
|
||||
- Ryan <46182144+ryanker@users.noreply.github.com>
|
||||
- Ryan J. Yoder <me@ryanjyoder.com>
|
||||
- SRK.Lyu <superalsrk@gmail.com>
|
||||
- Sai <sairoutine@gmail.com>
|
||||
- Samuel Abreu <sdepaula@gmail.com>
|
||||
- Santhosh Kumar <santhoshkumarr1096@gmail.com>
|
||||
- Sasha Melentyev <sasha@melentyev.io>
|
||||
- Sasha Myasoedov <msoedov@gmail.com>
|
||||
- Segev Finer <segev208@gmail.com>
|
||||
- Sergey Egorov <egorovhome@gmail.com>
|
||||
- Sergey Fedchenko <seregayoga@bk.ru>
|
||||
- Sergey Gonimar <sergey.gonimar@gmail.com>
|
||||
- Sergey Ponomarev <me@sergey-ponomarev.ru>
|
||||
- Serica <943914044@qq.com>
|
||||
- Shamus Taylor <Shamus03@me.com>
|
||||
- Shilin Wang <jarvisfironman@gmail.com>
|
||||
- Shuo <openset.wang@gmail.com>
|
||||
- Skuli Oskarsson <skuli@codeiak.io>
|
||||
- Snawoot <vladislav-ex-github@vm-0.com>
|
||||
- Sridhar Ratnakumar <srid@srid.ca>
|
||||
- Steeve Chailloux <steeve@chaahk.com>
|
||||
- Sudhir Mishra <sudhirxps@gmail.com>
|
||||
- Suhas Karanth <sudo-suhas@users.noreply.github.com>
|
||||
- TaeJun Park <miking38@gmail.com>
|
||||
- Tatsuya Hoshino <tatsuya7.hoshino7@gmail.com>
|
||||
- Tevic <tevic.tt@gmail.com>
|
||||
- Tevin Jeffrey <tev.jeffrey@gmail.com>
|
||||
- The Gitter Badger <badger@gitter.im>
|
||||
- Thibault Jamet <tjamet@users.noreply.github.com>
|
||||
- Thomas Boerger <thomas@webhippie.de>
|
||||
- Thomas Schaffer <loopfz@gmail.com>
|
||||
- Tommy Chu <tommychu2256@gmail.com>
|
||||
- Tudor Roman <tudurom@gmail.com>
|
||||
- Uwe Dauernheim <djui@users.noreply.github.com>
|
||||
- Valentine Oragbakosi <valentine13400@gmail.com>
|
||||
- Vas N <pnvasanth@users.noreply.github.com>
|
||||
- Vasilyuk Vasiliy <By-Vasiliy@users.noreply.github.com>
|
||||
- Victor Castell <victor@victorcastell.com>
|
||||
- Vince Yuan <vince.yuan@gmail.com>
|
||||
- Vyacheslav Dubinin <vyacheslav.dubinin@gmail.com>
|
||||
- Waynerv <ampedee@gmail.com>
|
||||
- Weilin Shi <934587911@qq.com>
|
||||
- Xudong Cai <fifsky@gmail.com>
|
||||
- Yasuhiro Matsumoto <mattn.jp@gmail.com>
|
||||
- Yehezkiel Syamsuhadi <ybs@ybs.im>
|
||||
- Yoshiki Nakagawa <yyoshiki41@gmail.com>
|
||||
- Yoshiyuki Kinjo <yskkin+github@gmail.com>
|
||||
- Yue Yang <g1enyy0ung@gmail.com>
|
||||
- ZYunH <zyunhjob@163.com>
|
||||
- Zach Newburgh <zach.newburgh@gmail.com>
|
||||
- Zasda Yusuf Mikail <zasdaym@gmail.com>
|
||||
- ZhangYunHao <zyunhjob@163.com>
|
||||
- ZhiFeng Hu <hufeng1987@gmail.com>
|
||||
- Zhu Xi <zhuxi910511@163.com>
|
||||
- a2tt <usera2tt@gmail.com>
|
||||
- ahuigo <1781999+ahuigo@users.noreply.github.com>
|
||||
- ali <anio@users.noreply.github.com>
|
||||
- aljun <salameryy@163.com>
|
||||
- andrea <crypto.andrea@protonmail.ch>
|
||||
- andriikushch <andrii.kushch@gmail.com>
|
||||
- anoty <anjunyou@foxmail.com>
|
||||
- awkj <hzzbiu@gmail.com>
|
||||
- axiaoxin <254606826@qq.com>
|
||||
- bbiao <bbbiao@gmail.com>
|
||||
- bestgopher <84328409@qq.com>
|
||||
- betahu <zhong.wenhuang@foxmail.com>
|
||||
- bigwheel <k.bigwheel+eng@gmail.com>
|
||||
- bn4t <17193640+bn4t@users.noreply.github.com>
|
||||
- bullgare <bullgare@gmail.com>
|
||||
- chainhelen <chainhelen@gmail.com>
|
||||
- chenyang929 <chenyang929code@gmail.com>
|
||||
- chriswhelix <chris.williams@helix.com>
|
||||
- collinmsn <4130944@qq.com>
|
||||
- cssivision <cssivision@gmail.com>
|
||||
- danielalves <alves.lopes.dan@gmail.com>
|
||||
- delphinus <delphinus@remora.cx>
|
||||
- dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
|
||||
- dickeyxxx <jeff@dickeyxxx.com>
|
||||
- edebernis <emeric.debernis@gmail.com>
|
||||
- error10 <error@ioerror.us>
|
||||
- esplo <esplo@users.noreply.github.com>
|
||||
- eudore <30709860+eudore@users.noreply.github.com>
|
||||
- ffhelicopter <32922889+ffhelicopter@users.noreply.github.com>
|
||||
- filikos <11477309+filikos@users.noreply.github.com>
|
||||
- forging2012 <forging2012@users.noreply.github.com>
|
||||
- goqihoo <goqihoo@gmail.com>
|
||||
- grapeVine <treeui.old@gmail.com>
|
||||
- guonaihong <guonaihong@qq.com>
|
||||
- heige <daheige@users.noreply.github.com>
|
||||
- heige <zhuwei313@hotmail.com>
|
||||
- hellojukay <hellojukay@163.com>
|
||||
- henrylee2cn <henrylee2cn@gmail.com>
|
||||
- htobenothing <htobenothing@gmail.com>
|
||||
- iamhesir <78344375+iamhesir@users.noreply.github.com>
|
||||
- ijaa <kailiu2013@gmail.com>
|
||||
- ishanray <ishan.iipm@gmail.com>
|
||||
- ishanray <ishanray@users.noreply.github.com>
|
||||
- itcloudy <272685110@qq.com>
|
||||
- jarodsong6 <jarodsong6@gmail.com>
|
||||
- jasonrhansen <jasonrodneyhansen@gmail.com>
|
||||
- jincheng9 <perfume0607@gmail.com>
|
||||
- joeADSP <75027008+joeADSP@users.noreply.github.com>
|
||||
- junfengye <junfeng.yejf@gmail.com>
|
||||
- kaiiak <aNxFi37X@outlook.com>
|
||||
- kebo <kevinke2020@outlook.com>
|
||||
- keke <19yamashita15@gmail.com>
|
||||
- kishor kunal raj <68464660+kishorkunal-raj@users.noreply.github.com>
|
||||
- kyledinh <kyledinh@gmail.com>
|
||||
- lantw44 <lantw44@gmail.com>
|
||||
- likakuli <1154584512@qq.com>
|
||||
- linfangrong <linfangrong.liuxin@qq.com>
|
||||
- linzi <873804682@qq.com>
|
||||
- llgoer <yanghuxiao@vip.qq.com>
|
||||
- long-road <13412081338@163.com>
|
||||
- mbesancon <mathieu.besancon@gmail.com>
|
||||
- mehdy <mehdy.khoshnoody@gmail.com>
|
||||
- metal A-wing <freedom.awing.777@gmail.com>
|
||||
- micanzhang <micanzhang@gmail.com>
|
||||
- minarc <ragnhildmowinckel@gmail.com>
|
||||
- mllu <mornlyn@gmail.com>
|
||||
- mopemoepe <yutaka.matsubara@gmail.com>
|
||||
- msoedov <msoedov@gmail.com>
|
||||
- mstmdev <mstmdev@gmail.com>
|
||||
- novaeye <fcoffee@gmail.com>
|
||||
- olebedev <oolebedev@gmail.com>
|
||||
- phithon <phith0n@users.noreply.github.com>
|
||||
- pjgg <pablo.gonzalez.granados@gmail.com>
|
||||
- qm012 <67568757+qm012@users.noreply.github.com>
|
||||
- raymonder jin <rayjingithub@gmail.com>
|
||||
- rns <ruslan.shvedov@gmail.com>
|
||||
- root@andrea:~# <crypto.andrea@protonmail.ch>
|
||||
- sekky0905 <20237968+sekky0905@users.noreply.github.com>
|
||||
- senhtry <w169q169@gmail.com>
|
||||
- shadrus <shadrus@gmail.com>
|
||||
- silasb <silas.baronda@gmail.com>
|
||||
- solos <lxl1217@gmail.com>
|
||||
- songjiayang <songjiayang@users.noreply.github.com>
|
||||
- sope <shenshouer@163.com>
|
||||
- srt180 <30768686+srt180@users.noreply.github.com>
|
||||
- stackerzzq <foo_stacker@yeah.net>
|
||||
- sunshineplan <sunshineplan@users.noreply.github.com>
|
||||
- syssam <s.y.s.sam.sys@gmail.com>
|
||||
- techjanitor <puntme@gmail.com>
|
||||
- techjanitor <techjanitor@users.noreply.github.com>
|
||||
- thinkerou <thinkerou@gmail.com>
|
||||
- thinkgo <49174849+thinkgos@users.noreply.github.com>
|
||||
- tsirolnik <tsirolnik@users.noreply.github.com>
|
||||
- tyltr <31768692+tylitianrui@users.noreply.github.com>
|
||||
- vinhha96 <anhvinha1@gmail.com>
|
||||
- voidman <retmain@foxmail.com>
|
||||
- vz <vzvway@gmail.com>
|
||||
- wei <wei840222@gmail.com>
|
||||
- weibaohui <weibaohui@yeah.net>
|
||||
- whirosan <whirosan@users.noreply.github.com>
|
||||
- willnewrelic <will@newrelic.com>
|
||||
- wssccc <wssccc@qq.com>
|
||||
- wuhuizuo <wuhuizuo@126.com>
|
||||
- xyb <xyb4638@gmail.com>
|
||||
- y-yagi <yuuji.yaginuma@gmail.com>
|
||||
- yiranzai <wuqingdzx@gmail.com>
|
||||
- youzeliang <youzel@126.com>
|
||||
- yugu <chenzilong_1227@foxmail.com>
|
||||
- yuyabe <yuyabee@gmail.com>
|
||||
- zebozhuang <zebozhuang@163.com>
|
||||
- zero11-0203 <93071220+zero11-0203@users.noreply.github.com>
|
||||
- zesani <7sin@outlook.co.th>
|
||||
- zhanweidu <zhanweidu@163.com>
|
||||
- zhing <zqwillseven@gmail.com>
|
||||
- ziheng <zihenglv@gmail.com>
|
||||
- zzjin <zzjin@users.noreply.github.com>
|
||||
- 森 優太 <59682979+uta-mori@users.noreply.github.com>
|
||||
- 杰哥 <858806258@qq.com>
|
||||
- 涛叔 <hi@taoshu.in>
|
||||
- 市民233 <mengrenxiong@gmail.com>
|
||||
- 尹宝强 <wmdandme@gmail.com>
|
||||
- 梦溪笔谈 <loongmxbt@gmail.com>
|
||||
- 飞雪无情 <ls8707@gmail.com>
|
||||
- 寻寻觅觅的Gopher <zoujh99@qq.com>
|
666
BENCHMARKS.md
666
BENCHMARKS.md
|
@ -1,666 +0,0 @@
|
|||
|
||||
# Benchmark System
|
||||
|
||||
**VM HOST:** Travis
|
||||
**Machine:** Ubuntu 16.04.6 LTS x64
|
||||
**Date:** May 04th, 2020
|
||||
**Version:** Gin v1.6.3
|
||||
**Go Version:** 1.14.2 linux/amd64
|
||||
**Source:** [Go HTTP Router Benchmark](https://github.com/gin-gonic/go-http-routing-benchmark)
|
||||
**Result:** [See the gist](https://gist.github.com/appleboy/b5f2ecfaf50824ae9c64dcfb9165ae5e) or [Travis result](https://travis-ci.org/github/gin-gonic/go-http-routing-benchmark/jobs/682947061)
|
||||
|
||||
## Static Routes: 157
|
||||
|
||||
```sh
|
||||
Gin: 34936 Bytes
|
||||
|
||||
HttpServeMux: 14512 Bytes
|
||||
Ace: 30680 Bytes
|
||||
Aero: 34536 Bytes
|
||||
Bear: 30456 Bytes
|
||||
Beego: 98456 Bytes
|
||||
Bone: 40224 Bytes
|
||||
Chi: 83608 Bytes
|
||||
Denco: 10216 Bytes
|
||||
Echo: 80328 Bytes
|
||||
GocraftWeb: 55288 Bytes
|
||||
Goji: 29744 Bytes
|
||||
Gojiv2: 105840 Bytes
|
||||
GoJsonRest: 137496 Bytes
|
||||
GoRestful: 816936 Bytes
|
||||
GorillaMux: 585632 Bytes
|
||||
GowwwRouter: 24968 Bytes
|
||||
HttpRouter: 21712 Bytes
|
||||
HttpTreeMux: 73448 Bytes
|
||||
Kocha: 115472 Bytes
|
||||
LARS: 30640 Bytes
|
||||
Macaron: 38592 Bytes
|
||||
Martini: 310864 Bytes
|
||||
Pat: 19696 Bytes
|
||||
Possum: 89920 Bytes
|
||||
R2router: 23712 Bytes
|
||||
Rivet: 24608 Bytes
|
||||
Tango: 28264 Bytes
|
||||
TigerTonic: 78768 Bytes
|
||||
Traffic: 538976 Bytes
|
||||
Vulcan: 369960 Bytes
|
||||
```
|
||||
|
||||
## GithubAPI Routes: 203
|
||||
|
||||
```sh
|
||||
Gin: 58512 Bytes
|
||||
|
||||
Ace: 48688 Bytes
|
||||
Aero: 318568 Bytes
|
||||
Bear: 84248 Bytes
|
||||
Beego: 150936 Bytes
|
||||
Bone: 100976 Bytes
|
||||
Chi: 95112 Bytes
|
||||
Denco: 36736 Bytes
|
||||
Echo: 100296 Bytes
|
||||
GocraftWeb: 95432 Bytes
|
||||
Goji: 49680 Bytes
|
||||
Gojiv2: 104704 Bytes
|
||||
GoJsonRest: 141976 Bytes
|
||||
GoRestful: 1241656 Bytes
|
||||
GorillaMux: 1322784 Bytes
|
||||
GowwwRouter: 80008 Bytes
|
||||
HttpRouter: 37144 Bytes
|
||||
HttpTreeMux: 78800 Bytes
|
||||
Kocha: 785120 Bytes
|
||||
LARS: 48600 Bytes
|
||||
Macaron: 92784 Bytes
|
||||
Martini: 485264 Bytes
|
||||
Pat: 21200 Bytes
|
||||
Possum: 85312 Bytes
|
||||
R2router: 47104 Bytes
|
||||
Rivet: 42840 Bytes
|
||||
Tango: 54840 Bytes
|
||||
TigerTonic: 95264 Bytes
|
||||
Traffic: 921744 Bytes
|
||||
Vulcan: 425992 Bytes
|
||||
```
|
||||
|
||||
## GPlusAPI Routes: 13
|
||||
|
||||
```sh
|
||||
Gin: 4384 Bytes
|
||||
|
||||
Ace: 3712 Bytes
|
||||
Aero: 26056 Bytes
|
||||
Bear: 7112 Bytes
|
||||
Beego: 10272 Bytes
|
||||
Bone: 6688 Bytes
|
||||
Chi: 8024 Bytes
|
||||
Denco: 3264 Bytes
|
||||
Echo: 9688 Bytes
|
||||
GocraftWeb: 7496 Bytes
|
||||
Goji: 3152 Bytes
|
||||
Gojiv2: 7376 Bytes
|
||||
GoJsonRest: 11400 Bytes
|
||||
GoRestful: 74328 Bytes
|
||||
GorillaMux: 66208 Bytes
|
||||
GowwwRouter: 5744 Bytes
|
||||
HttpRouter: 2808 Bytes
|
||||
HttpTreeMux: 7440 Bytes
|
||||
Kocha: 128880 Bytes
|
||||
LARS: 3656 Bytes
|
||||
Macaron: 8656 Bytes
|
||||
Martini: 23920 Bytes
|
||||
Pat: 1856 Bytes
|
||||
Possum: 7248 Bytes
|
||||
R2router: 3928 Bytes
|
||||
Rivet: 3064 Bytes
|
||||
Tango: 5168 Bytes
|
||||
TigerTonic: 9408 Bytes
|
||||
Traffic: 46400 Bytes
|
||||
Vulcan: 25544 Bytes
|
||||
```
|
||||
|
||||
## ParseAPI Routes: 26
|
||||
|
||||
```sh
|
||||
Gin: 7776 Bytes
|
||||
|
||||
Ace: 6704 Bytes
|
||||
Aero: 28488 Bytes
|
||||
Bear: 12320 Bytes
|
||||
Beego: 19280 Bytes
|
||||
Bone: 11440 Bytes
|
||||
Chi: 9744 Bytes
|
||||
Denco: 4192 Bytes
|
||||
Echo: 11664 Bytes
|
||||
GocraftWeb: 12800 Bytes
|
||||
Goji: 5680 Bytes
|
||||
Gojiv2: 14464 Bytes
|
||||
GoJsonRest: 14072 Bytes
|
||||
GoRestful: 116264 Bytes
|
||||
GorillaMux: 105880 Bytes
|
||||
GowwwRouter: 9344 Bytes
|
||||
HttpRouter: 5072 Bytes
|
||||
HttpTreeMux: 7848 Bytes
|
||||
Kocha: 181712 Bytes
|
||||
LARS: 6632 Bytes
|
||||
Macaron: 13648 Bytes
|
||||
Martini: 45888 Bytes
|
||||
Pat: 2560 Bytes
|
||||
Possum: 9200 Bytes
|
||||
R2router: 7056 Bytes
|
||||
Rivet: 5680 Bytes
|
||||
Tango: 8920 Bytes
|
||||
TigerTonic: 9840 Bytes
|
||||
Traffic: 79096 Bytes
|
||||
Vulcan: 44504 Bytes
|
||||
```
|
||||
|
||||
## Static Routes
|
||||
|
||||
```sh
|
||||
BenchmarkGin_StaticAll 62169 19319 ns/op 0 B/op 0 allocs/op
|
||||
|
||||
BenchmarkAce_StaticAll 65428 18313 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAero_StaticAll 121132 9632 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpServeMux_StaticAll 52626 22758 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBeego_StaticAll 9962 179058 ns/op 55264 B/op 471 allocs/op
|
||||
BenchmarkBear_StaticAll 14894 80966 ns/op 20272 B/op 469 allocs/op
|
||||
BenchmarkBone_StaticAll 18718 64065 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkChi_StaticAll 10000 149827 ns/op 67824 B/op 471 allocs/op
|
||||
BenchmarkDenco_StaticAll 211393 5680 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkEcho_StaticAll 49341 24343 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_StaticAll 10000 126209 ns/op 46312 B/op 785 allocs/op
|
||||
BenchmarkGoji_StaticAll 27956 43174 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGojiv2_StaticAll 3430 370718 ns/op 205984 B/op 1570 allocs/op
|
||||
BenchmarkGoJsonRest_StaticAll 9134 188888 ns/op 51653 B/op 1727 allocs/op
|
||||
BenchmarkGoRestful_StaticAll 706 1703330 ns/op 613280 B/op 2053 allocs/op
|
||||
BenchmarkGorillaMux_StaticAll 1268 924083 ns/op 153233 B/op 1413 allocs/op
|
||||
BenchmarkGowwwRouter_StaticAll 63374 18935 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpRouter_StaticAll 109938 10902 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_StaticAll 109166 10861 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkKocha_StaticAll 92258 12992 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkLARS_StaticAll 65200 18387 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_StaticAll 5671 291501 ns/op 115553 B/op 1256 allocs/op
|
||||
BenchmarkMartini_StaticAll 807 1460498 ns/op 125444 B/op 1717 allocs/op
|
||||
BenchmarkPat_StaticAll 513 2342396 ns/op 602832 B/op 12559 allocs/op
|
||||
BenchmarkPossum_StaticAll 10000 128270 ns/op 65312 B/op 471 allocs/op
|
||||
BenchmarkR2router_StaticAll 16726 71760 ns/op 22608 B/op 628 allocs/op
|
||||
BenchmarkRivet_StaticAll 41722 28723 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkTango_StaticAll 7606 205082 ns/op 39209 B/op 1256 allocs/op
|
||||
BenchmarkTigerTonic_StaticAll 26247 45806 ns/op 7376 B/op 157 allocs/op
|
||||
BenchmarkTraffic_StaticAll 550 2284518 ns/op 754864 B/op 14601 allocs/op
|
||||
BenchmarkVulcan_StaticAll 10000 131343 ns/op 15386 B/op 471 allocs/op
|
||||
```
|
||||
|
||||
## Micro Benchmarks
|
||||
|
||||
```sh
|
||||
BenchmarkGin_Param 18785022 63.9 ns/op 0 B/op 0 allocs/op
|
||||
|
||||
BenchmarkAce_Param 14689765 81.5 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAero_Param 23094770 51.2 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBear_Param 1417045 845 ns/op 456 B/op 5 allocs/op
|
||||
BenchmarkBeego_Param 1000000 1080 ns/op 352 B/op 3 allocs/op
|
||||
BenchmarkBone_Param 1000000 1463 ns/op 816 B/op 6 allocs/op
|
||||
BenchmarkChi_Param 1378756 885 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkDenco_Param 8557899 143 ns/op 32 B/op 1 allocs/op
|
||||
BenchmarkEcho_Param 16433347 75.5 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_Param 1000000 1218 ns/op 648 B/op 8 allocs/op
|
||||
BenchmarkGoji_Param 1921248 617 ns/op 336 B/op 2 allocs/op
|
||||
BenchmarkGojiv2_Param 561848 2156 ns/op 1328 B/op 11 allocs/op
|
||||
BenchmarkGoJsonRest_Param 1000000 1358 ns/op 649 B/op 13 allocs/op
|
||||
BenchmarkGoRestful_Param 224857 5307 ns/op 4192 B/op 14 allocs/op
|
||||
BenchmarkGorillaMux_Param 498313 2459 ns/op 1280 B/op 10 allocs/op
|
||||
BenchmarkGowwwRouter_Param 1864354 654 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkHttpRouter_Param 26269074 47.7 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_Param 2109829 557 ns/op 352 B/op 3 allocs/op
|
||||
BenchmarkKocha_Param 5050216 243 ns/op 56 B/op 3 allocs/op
|
||||
BenchmarkLARS_Param 19811712 59.9 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_Param 662746 2329 ns/op 1072 B/op 10 allocs/op
|
||||
BenchmarkMartini_Param 279902 4260 ns/op 1072 B/op 10 allocs/op
|
||||
BenchmarkPat_Param 1000000 1382 ns/op 536 B/op 11 allocs/op
|
||||
BenchmarkPossum_Param 1000000 1014 ns/op 496 B/op 5 allocs/op
|
||||
BenchmarkR2router_Param 1712559 707 ns/op 432 B/op 5 allocs/op
|
||||
BenchmarkRivet_Param 6648086 182 ns/op 48 B/op 1 allocs/op
|
||||
BenchmarkTango_Param 1221504 994 ns/op 248 B/op 8 allocs/op
|
||||
BenchmarkTigerTonic_Param 891661 2261 ns/op 776 B/op 16 allocs/op
|
||||
BenchmarkTraffic_Param 350059 3598 ns/op 1856 B/op 21 allocs/op
|
||||
BenchmarkVulcan_Param 2517823 472 ns/op 98 B/op 3 allocs/op
|
||||
BenchmarkAce_Param5 9214365 130 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAero_Param5 15369013 77.9 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBear_Param5 1000000 1113 ns/op 501 B/op 5 allocs/op
|
||||
BenchmarkBeego_Param5 1000000 1269 ns/op 352 B/op 3 allocs/op
|
||||
BenchmarkBone_Param5 986820 1873 ns/op 864 B/op 6 allocs/op
|
||||
BenchmarkChi_Param5 1000000 1156 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkDenco_Param5 3036331 400 ns/op 160 B/op 1 allocs/op
|
||||
BenchmarkEcho_Param5 6447133 186 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGin_Param5 10786068 110 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_Param5 844820 1944 ns/op 920 B/op 11 allocs/op
|
||||
BenchmarkGoji_Param5 1474965 827 ns/op 336 B/op 2 allocs/op
|
||||
BenchmarkGojiv2_Param5 442820 2516 ns/op 1392 B/op 11 allocs/op
|
||||
BenchmarkGoJsonRest_Param5 507555 2711 ns/op 1097 B/op 16 allocs/op
|
||||
BenchmarkGoRestful_Param5 216481 6093 ns/op 4288 B/op 14 allocs/op
|
||||
BenchmarkGorillaMux_Param5 314402 3628 ns/op 1344 B/op 10 allocs/op
|
||||
BenchmarkGowwwRouter_Param5 1624660 733 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkHttpRouter_Param5 13167324 92.0 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_Param5 1000000 1295 ns/op 576 B/op 6 allocs/op
|
||||
BenchmarkKocha_Param5 1000000 1138 ns/op 440 B/op 10 allocs/op
|
||||
BenchmarkLARS_Param5 11580613 105 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_Param5 473596 2755 ns/op 1072 B/op 10 allocs/op
|
||||
BenchmarkMartini_Param5 230756 5111 ns/op 1232 B/op 11 allocs/op
|
||||
BenchmarkPat_Param5 469190 3370 ns/op 888 B/op 29 allocs/op
|
||||
BenchmarkPossum_Param5 1000000 1002 ns/op 496 B/op 5 allocs/op
|
||||
BenchmarkR2router_Param5 1422129 844 ns/op 432 B/op 5 allocs/op
|
||||
BenchmarkRivet_Param5 2263789 539 ns/op 240 B/op 1 allocs/op
|
||||
BenchmarkTango_Param5 1000000 1256 ns/op 360 B/op 8 allocs/op
|
||||
BenchmarkTigerTonic_Param5 175500 7492 ns/op 2279 B/op 39 allocs/op
|
||||
BenchmarkTraffic_Param5 233631 5816 ns/op 2208 B/op 27 allocs/op
|
||||
BenchmarkVulcan_Param5 1923416 629 ns/op 98 B/op 3 allocs/op
|
||||
BenchmarkAce_Param20 4321266 281 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAero_Param20 31501641 35.2 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBear_Param20 335204 3489 ns/op 1665 B/op 5 allocs/op
|
||||
BenchmarkBeego_Param20 503674 2860 ns/op 352 B/op 3 allocs/op
|
||||
BenchmarkBone_Param20 298922 4741 ns/op 2031 B/op 6 allocs/op
|
||||
BenchmarkChi_Param20 878181 1957 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkDenco_Param20 1000000 1360 ns/op 640 B/op 1 allocs/op
|
||||
BenchmarkEcho_Param20 2104946 580 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGin_Param20 4167204 290 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_Param20 173064 7514 ns/op 3796 B/op 15 allocs/op
|
||||
BenchmarkGoji_Param20 458778 2651 ns/op 1247 B/op 2 allocs/op
|
||||
BenchmarkGojiv2_Param20 364862 3178 ns/op 1632 B/op 11 allocs/op
|
||||
BenchmarkGoJsonRest_Param20 125514 9760 ns/op 4485 B/op 20 allocs/op
|
||||
BenchmarkGoRestful_Param20 101217 11964 ns/op 6715 B/op 18 allocs/op
|
||||
BenchmarkGorillaMux_Param20 147654 8132 ns/op 3452 B/op 12 allocs/op
|
||||
BenchmarkGowwwRouter_Param20 1000000 1225 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkHttpRouter_Param20 4920895 247 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_Param20 173202 6605 ns/op 3196 B/op 10 allocs/op
|
||||
BenchmarkKocha_Param20 345988 3620 ns/op 1808 B/op 27 allocs/op
|
||||
BenchmarkLARS_Param20 4592326 262 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_Param20 166492 7286 ns/op 2924 B/op 12 allocs/op
|
||||
BenchmarkMartini_Param20 122162 10653 ns/op 3595 B/op 13 allocs/op
|
||||
BenchmarkPat_Param20 78630 15239 ns/op 4424 B/op 93 allocs/op
|
||||
BenchmarkPossum_Param20 1000000 1008 ns/op 496 B/op 5 allocs/op
|
||||
BenchmarkR2router_Param20 294981 4587 ns/op 2284 B/op 7 allocs/op
|
||||
BenchmarkRivet_Param20 691798 2090 ns/op 1024 B/op 1 allocs/op
|
||||
BenchmarkTango_Param20 842440 2505 ns/op 856 B/op 8 allocs/op
|
||||
BenchmarkTigerTonic_Param20 38614 31509 ns/op 9870 B/op 119 allocs/op
|
||||
BenchmarkTraffic_Param20 57633 21107 ns/op 7853 B/op 47 allocs/op
|
||||
BenchmarkVulcan_Param20 1000000 1178 ns/op 98 B/op 3 allocs/op
|
||||
BenchmarkAce_ParamWrite 7330743 180 ns/op 8 B/op 1 allocs/op
|
||||
BenchmarkAero_ParamWrite 13833598 86.7 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBear_ParamWrite 1363321 867 ns/op 456 B/op 5 allocs/op
|
||||
BenchmarkBeego_ParamWrite 1000000 1104 ns/op 360 B/op 4 allocs/op
|
||||
BenchmarkBone_ParamWrite 1000000 1475 ns/op 816 B/op 6 allocs/op
|
||||
BenchmarkChi_ParamWrite 1320590 892 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkDenco_ParamWrite 7093605 172 ns/op 32 B/op 1 allocs/op
|
||||
BenchmarkEcho_ParamWrite 8434424 161 ns/op 8 B/op 1 allocs/op
|
||||
BenchmarkGin_ParamWrite 10377034 118 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_ParamWrite 1000000 1266 ns/op 656 B/op 9 allocs/op
|
||||
BenchmarkGoji_ParamWrite 1874168 654 ns/op 336 B/op 2 allocs/op
|
||||
BenchmarkGojiv2_ParamWrite 459032 2352 ns/op 1360 B/op 13 allocs/op
|
||||
BenchmarkGoJsonRest_ParamWrite 499434 2145 ns/op 1128 B/op 18 allocs/op
|
||||
BenchmarkGoRestful_ParamWrite 241087 5470 ns/op 4200 B/op 15 allocs/op
|
||||
BenchmarkGorillaMux_ParamWrite 425686 2522 ns/op 1280 B/op 10 allocs/op
|
||||
BenchmarkGowwwRouter_ParamWrite 922172 1778 ns/op 976 B/op 8 allocs/op
|
||||
BenchmarkHttpRouter_ParamWrite 15392049 77.7 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_ParamWrite 1973385 597 ns/op 352 B/op 3 allocs/op
|
||||
BenchmarkKocha_ParamWrite 4262500 281 ns/op 56 B/op 3 allocs/op
|
||||
BenchmarkLARS_ParamWrite 10764410 113 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_ParamWrite 486769 2726 ns/op 1176 B/op 14 allocs/op
|
||||
BenchmarkMartini_ParamWrite 264804 4842 ns/op 1176 B/op 14 allocs/op
|
||||
BenchmarkPat_ParamWrite 735116 2047 ns/op 960 B/op 15 allocs/op
|
||||
BenchmarkPossum_ParamWrite 1000000 1004 ns/op 496 B/op 5 allocs/op
|
||||
BenchmarkR2router_ParamWrite 1592136 768 ns/op 432 B/op 5 allocs/op
|
||||
BenchmarkRivet_ParamWrite 3582051 339 ns/op 112 B/op 2 allocs/op
|
||||
BenchmarkTango_ParamWrite 2237337 534 ns/op 136 B/op 4 allocs/op
|
||||
BenchmarkTigerTonic_ParamWrite 439608 3136 ns/op 1216 B/op 21 allocs/op
|
||||
BenchmarkTraffic_ParamWrite 306979 4328 ns/op 2280 B/op 25 allocs/op
|
||||
BenchmarkVulcan_ParamWrite 2529973 472 ns/op 98 B/op 3 allocs/op
|
||||
```
|
||||
|
||||
## GitHub
|
||||
|
||||
```sh
|
||||
BenchmarkGin_GithubStatic 15629472 76.7 ns/op 0 B/op 0 allocs/op
|
||||
|
||||
BenchmarkAce_GithubStatic 15542612 75.9 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAero_GithubStatic 24777151 48.5 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBear_GithubStatic 2788894 435 ns/op 120 B/op 3 allocs/op
|
||||
BenchmarkBeego_GithubStatic 1000000 1064 ns/op 352 B/op 3 allocs/op
|
||||
BenchmarkBone_GithubStatic 93507 12838 ns/op 2880 B/op 60 allocs/op
|
||||
BenchmarkChi_GithubStatic 1387743 860 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkDenco_GithubStatic 39384996 30.4 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkEcho_GithubStatic 12076382 99.1 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_GithubStatic 1596495 756 ns/op 296 B/op 5 allocs/op
|
||||
BenchmarkGoji_GithubStatic 6364876 189 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGojiv2_GithubStatic 550202 2098 ns/op 1312 B/op 10 allocs/op
|
||||
BenchmarkGoRestful_GithubStatic 102183 12552 ns/op 4256 B/op 13 allocs/op
|
||||
BenchmarkGoJsonRest_GithubStatic 1000000 1029 ns/op 329 B/op 11 allocs/op
|
||||
BenchmarkGorillaMux_GithubStatic 255552 5190 ns/op 976 B/op 9 allocs/op
|
||||
BenchmarkGowwwRouter_GithubStatic 15531916 77.1 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpRouter_GithubStatic 27920724 43.1 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_GithubStatic 21448953 55.8 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkKocha_GithubStatic 21405310 56.0 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkLARS_GithubStatic 13625156 89.0 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_GithubStatic 1000000 1747 ns/op 736 B/op 8 allocs/op
|
||||
BenchmarkMartini_GithubStatic 187186 7326 ns/op 768 B/op 9 allocs/op
|
||||
BenchmarkPat_GithubStatic 109143 11563 ns/op 3648 B/op 76 allocs/op
|
||||
BenchmarkPossum_GithubStatic 1575898 770 ns/op 416 B/op 3 allocs/op
|
||||
BenchmarkR2router_GithubStatic 3046231 404 ns/op 144 B/op 4 allocs/op
|
||||
BenchmarkRivet_GithubStatic 11484826 105 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkTango_GithubStatic 1000000 1153 ns/op 248 B/op 8 allocs/op
|
||||
BenchmarkTigerTonic_GithubStatic 4929780 249 ns/op 48 B/op 1 allocs/op
|
||||
BenchmarkTraffic_GithubStatic 106351 11819 ns/op 4664 B/op 90 allocs/op
|
||||
BenchmarkVulcan_GithubStatic 1613271 722 ns/op 98 B/op 3 allocs/op
|
||||
BenchmarkAce_GithubParam 8386032 143 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAero_GithubParam 11816200 102 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBear_GithubParam 1000000 1012 ns/op 496 B/op 5 allocs/op
|
||||
BenchmarkBeego_GithubParam 1000000 1157 ns/op 352 B/op 3 allocs/op
|
||||
BenchmarkBone_GithubParam 184653 6912 ns/op 1888 B/op 19 allocs/op
|
||||
BenchmarkChi_GithubParam 1000000 1102 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkDenco_GithubParam 3484798 352 ns/op 128 B/op 1 allocs/op
|
||||
BenchmarkEcho_GithubParam 6337380 189 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGin_GithubParam 9132032 131 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_GithubParam 1000000 1446 ns/op 712 B/op 9 allocs/op
|
||||
BenchmarkGoji_GithubParam 1248640 977 ns/op 336 B/op 2 allocs/op
|
||||
BenchmarkGojiv2_GithubParam 383233 2784 ns/op 1408 B/op 13 allocs/op
|
||||
BenchmarkGoJsonRest_GithubParam 1000000 1991 ns/op 713 B/op 14 allocs/op
|
||||
BenchmarkGoRestful_GithubParam 76414 16015 ns/op 4352 B/op 16 allocs/op
|
||||
BenchmarkGorillaMux_GithubParam 150026 7663 ns/op 1296 B/op 10 allocs/op
|
||||
BenchmarkGowwwRouter_GithubParam 1592044 751 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkHttpRouter_GithubParam 10420628 115 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_GithubParam 1403755 835 ns/op 384 B/op 4 allocs/op
|
||||
BenchmarkKocha_GithubParam 2286170 533 ns/op 128 B/op 5 allocs/op
|
||||
BenchmarkLARS_GithubParam 9540374 129 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_GithubParam 533154 2742 ns/op 1072 B/op 10 allocs/op
|
||||
BenchmarkMartini_GithubParam 119397 9638 ns/op 1152 B/op 11 allocs/op
|
||||
BenchmarkPat_GithubParam 150675 8858 ns/op 2408 B/op 48 allocs/op
|
||||
BenchmarkPossum_GithubParam 1000000 1001 ns/op 496 B/op 5 allocs/op
|
||||
BenchmarkR2router_GithubParam 1602886 761 ns/op 432 B/op 5 allocs/op
|
||||
BenchmarkRivet_GithubParam 2986579 409 ns/op 96 B/op 1 allocs/op
|
||||
BenchmarkTango_GithubParam 1000000 1356 ns/op 344 B/op 8 allocs/op
|
||||
BenchmarkTigerTonic_GithubParam 388899 3429 ns/op 1176 B/op 22 allocs/op
|
||||
BenchmarkTraffic_GithubParam 123160 9734 ns/op 2816 B/op 40 allocs/op
|
||||
BenchmarkVulcan_GithubParam 1000000 1138 ns/op 98 B/op 3 allocs/op
|
||||
BenchmarkAce_GithubAll 40543 29670 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAero_GithubAll 57632 20648 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBear_GithubAll 9234 216179 ns/op 86448 B/op 943 allocs/op
|
||||
BenchmarkBeego_GithubAll 7407 243496 ns/op 71456 B/op 609 allocs/op
|
||||
BenchmarkBone_GithubAll 420 2922835 ns/op 720160 B/op 8620 allocs/op
|
||||
BenchmarkChi_GithubAll 7620 238331 ns/op 87696 B/op 609 allocs/op
|
||||
BenchmarkDenco_GithubAll 18355 64494 ns/op 20224 B/op 167 allocs/op
|
||||
BenchmarkEcho_GithubAll 31251 38479 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGin_GithubAll 43550 27364 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_GithubAll 4117 300062 ns/op 131656 B/op 1686 allocs/op
|
||||
BenchmarkGoji_GithubAll 3274 416158 ns/op 56112 B/op 334 allocs/op
|
||||
BenchmarkGojiv2_GithubAll 1402 870518 ns/op 352720 B/op 4321 allocs/op
|
||||
BenchmarkGoJsonRest_GithubAll 2976 401507 ns/op 134371 B/op 2737 allocs/op
|
||||
BenchmarkGoRestful_GithubAll 410 2913158 ns/op 910144 B/op 2938 allocs/op
|
||||
BenchmarkGorillaMux_GithubAll 346 3384987 ns/op 251650 B/op 1994 allocs/op
|
||||
BenchmarkGowwwRouter_GithubAll 10000 143025 ns/op 72144 B/op 501 allocs/op
|
||||
BenchmarkHttpRouter_GithubAll 55938 21360 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_GithubAll 10000 153944 ns/op 65856 B/op 671 allocs/op
|
||||
BenchmarkKocha_GithubAll 10000 106315 ns/op 23304 B/op 843 allocs/op
|
||||
BenchmarkLARS_GithubAll 47779 25084 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_GithubAll 3266 371907 ns/op 149409 B/op 1624 allocs/op
|
||||
BenchmarkMartini_GithubAll 331 3444706 ns/op 226551 B/op 2325 allocs/op
|
||||
BenchmarkPat_GithubAll 273 4381818 ns/op 1483152 B/op 26963 allocs/op
|
||||
BenchmarkPossum_GithubAll 10000 164367 ns/op 84448 B/op 609 allocs/op
|
||||
BenchmarkR2router_GithubAll 10000 160220 ns/op 77328 B/op 979 allocs/op
|
||||
BenchmarkRivet_GithubAll 14625 82453 ns/op 16272 B/op 167 allocs/op
|
||||
BenchmarkTango_GithubAll 6255 279611 ns/op 63826 B/op 1618 allocs/op
|
||||
BenchmarkTigerTonic_GithubAll 2008 687874 ns/op 193856 B/op 4474 allocs/op
|
||||
BenchmarkTraffic_GithubAll 355 3478508 ns/op 820744 B/op 14114 allocs/op
|
||||
BenchmarkVulcan_GithubAll 6885 193333 ns/op 19894 B/op 609 allocs/op
|
||||
```
|
||||
|
||||
## Google+
|
||||
|
||||
```sh
|
||||
BenchmarkGin_GPlusStatic 19247326 62.2 ns/op 0 B/op 0 allocs/op
|
||||
|
||||
BenchmarkAce_GPlusStatic 20235060 59.2 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAero_GPlusStatic 31978935 37.6 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBear_GPlusStatic 3516523 341 ns/op 104 B/op 3 allocs/op
|
||||
BenchmarkBeego_GPlusStatic 1212036 991 ns/op 352 B/op 3 allocs/op
|
||||
BenchmarkBone_GPlusStatic 6736242 183 ns/op 32 B/op 1 allocs/op
|
||||
BenchmarkChi_GPlusStatic 1490640 814 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkDenco_GPlusStatic 55006856 21.8 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkEcho_GPlusStatic 17688258 67.9 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_GPlusStatic 1829181 666 ns/op 280 B/op 5 allocs/op
|
||||
BenchmarkGoji_GPlusStatic 9147451 130 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGojiv2_GPlusStatic 594015 2063 ns/op 1312 B/op 10 allocs/op
|
||||
BenchmarkGoJsonRest_GPlusStatic 1264906 950 ns/op 329 B/op 11 allocs/op
|
||||
BenchmarkGoRestful_GPlusStatic 231558 5341 ns/op 3872 B/op 13 allocs/op
|
||||
BenchmarkGorillaMux_GPlusStatic 908418 1809 ns/op 976 B/op 9 allocs/op
|
||||
BenchmarkGowwwRouter_GPlusStatic 40684604 29.5 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpRouter_GPlusStatic 46742804 25.7 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_GPlusStatic 32567161 36.9 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkKocha_GPlusStatic 33800060 35.3 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkLARS_GPlusStatic 20431858 60.0 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_GPlusStatic 1000000 1745 ns/op 736 B/op 8 allocs/op
|
||||
BenchmarkMartini_GPlusStatic 442248 3619 ns/op 768 B/op 9 allocs/op
|
||||
BenchmarkPat_GPlusStatic 4328004 292 ns/op 96 B/op 2 allocs/op
|
||||
BenchmarkPossum_GPlusStatic 1570753 763 ns/op 416 B/op 3 allocs/op
|
||||
BenchmarkR2router_GPlusStatic 3339474 355 ns/op 144 B/op 4 allocs/op
|
||||
BenchmarkRivet_GPlusStatic 18570961 64.7 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkTango_GPlusStatic 1388702 860 ns/op 200 B/op 8 allocs/op
|
||||
BenchmarkTigerTonic_GPlusStatic 7803543 159 ns/op 32 B/op 1 allocs/op
|
||||
BenchmarkTraffic_GPlusStatic 878605 2171 ns/op 1112 B/op 16 allocs/op
|
||||
BenchmarkVulcan_GPlusStatic 2742446 437 ns/op 98 B/op 3 allocs/op
|
||||
BenchmarkAce_GPlusParam 11626975 105 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAero_GPlusParam 16914322 71.6 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBear_GPlusParam 1405173 832 ns/op 480 B/op 5 allocs/op
|
||||
BenchmarkBeego_GPlusParam 1000000 1075 ns/op 352 B/op 3 allocs/op
|
||||
BenchmarkBone_GPlusParam 1000000 1557 ns/op 816 B/op 6 allocs/op
|
||||
BenchmarkChi_GPlusParam 1347926 894 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkDenco_GPlusParam 5513000 212 ns/op 64 B/op 1 allocs/op
|
||||
BenchmarkEcho_GPlusParam 11884383 101 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGin_GPlusParam 12898952 93.1 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_GPlusParam 1000000 1194 ns/op 648 B/op 8 allocs/op
|
||||
BenchmarkGoji_GPlusParam 1857229 645 ns/op 336 B/op 2 allocs/op
|
||||
BenchmarkGojiv2_GPlusParam 520939 2322 ns/op 1328 B/op 11 allocs/op
|
||||
BenchmarkGoJsonRest_GPlusParam 1000000 1536 ns/op 649 B/op 13 allocs/op
|
||||
BenchmarkGoRestful_GPlusParam 205449 5800 ns/op 4192 B/op 14 allocs/op
|
||||
BenchmarkGorillaMux_GPlusParam 395310 3188 ns/op 1280 B/op 10 allocs/op
|
||||
BenchmarkGowwwRouter_GPlusParam 1851798 667 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkHttpRouter_GPlusParam 18420789 65.2 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_GPlusParam 1878463 629 ns/op 352 B/op 3 allocs/op
|
||||
BenchmarkKocha_GPlusParam 4495610 273 ns/op 56 B/op 3 allocs/op
|
||||
BenchmarkLARS_GPlusParam 14615976 83.2 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_GPlusParam 584145 2549 ns/op 1072 B/op 10 allocs/op
|
||||
BenchmarkMartini_GPlusParam 250501 4583 ns/op 1072 B/op 10 allocs/op
|
||||
BenchmarkPat_GPlusParam 1000000 1645 ns/op 576 B/op 11 allocs/op
|
||||
BenchmarkPossum_GPlusParam 1000000 1008 ns/op 496 B/op 5 allocs/op
|
||||
BenchmarkR2router_GPlusParam 1708191 688 ns/op 432 B/op 5 allocs/op
|
||||
BenchmarkRivet_GPlusParam 5795014 211 ns/op 48 B/op 1 allocs/op
|
||||
BenchmarkTango_GPlusParam 1000000 1091 ns/op 264 B/op 8 allocs/op
|
||||
BenchmarkTigerTonic_GPlusParam 760221 2489 ns/op 856 B/op 16 allocs/op
|
||||
BenchmarkTraffic_GPlusParam 309774 4039 ns/op 1872 B/op 21 allocs/op
|
||||
BenchmarkVulcan_GPlusParam 1935730 623 ns/op 98 B/op 3 allocs/op
|
||||
BenchmarkAce_GPlus2Params 9158314 134 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAero_GPlus2Params 11300517 107 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBear_GPlus2Params 1239238 961 ns/op 496 B/op 5 allocs/op
|
||||
BenchmarkBeego_GPlus2Params 1000000 1202 ns/op 352 B/op 3 allocs/op
|
||||
BenchmarkBone_GPlus2Params 335576 3725 ns/op 1168 B/op 10 allocs/op
|
||||
BenchmarkChi_GPlus2Params 1000000 1014 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkDenco_GPlus2Params 4394598 280 ns/op 64 B/op 1 allocs/op
|
||||
BenchmarkEcho_GPlus2Params 7851861 154 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGin_GPlus2Params 9958588 120 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_GPlus2Params 1000000 1433 ns/op 712 B/op 9 allocs/op
|
||||
BenchmarkGoji_GPlus2Params 1325134 909 ns/op 336 B/op 2 allocs/op
|
||||
BenchmarkGojiv2_GPlus2Params 405955 2870 ns/op 1408 B/op 14 allocs/op
|
||||
BenchmarkGoJsonRest_GPlus2Params 977038 1987 ns/op 713 B/op 14 allocs/op
|
||||
BenchmarkGoRestful_GPlus2Params 205018 6142 ns/op 4384 B/op 16 allocs/op
|
||||
BenchmarkGorillaMux_GPlus2Params 205641 6015 ns/op 1296 B/op 10 allocs/op
|
||||
BenchmarkGowwwRouter_GPlus2Params 1748542 684 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkHttpRouter_GPlus2Params 14047102 87.7 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_GPlus2Params 1418673 828 ns/op 384 B/op 4 allocs/op
|
||||
BenchmarkKocha_GPlus2Params 2334562 520 ns/op 128 B/op 5 allocs/op
|
||||
BenchmarkLARS_GPlus2Params 11954094 101 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_GPlus2Params 491552 2890 ns/op 1072 B/op 10 allocs/op
|
||||
BenchmarkMartini_GPlus2Params 120532 9545 ns/op 1200 B/op 13 allocs/op
|
||||
BenchmarkPat_GPlus2Params 194739 6766 ns/op 2168 B/op 33 allocs/op
|
||||
BenchmarkPossum_GPlus2Params 1201224 1009 ns/op 496 B/op 5 allocs/op
|
||||
BenchmarkR2router_GPlus2Params 1575535 756 ns/op 432 B/op 5 allocs/op
|
||||
BenchmarkRivet_GPlus2Params 3698930 325 ns/op 96 B/op 1 allocs/op
|
||||
BenchmarkTango_GPlus2Params 1000000 1212 ns/op 344 B/op 8 allocs/op
|
||||
BenchmarkTigerTonic_GPlus2Params 349350 3660 ns/op 1200 B/op 22 allocs/op
|
||||
BenchmarkTraffic_GPlus2Params 169714 7862 ns/op 2248 B/op 28 allocs/op
|
||||
BenchmarkVulcan_GPlus2Params 1222288 974 ns/op 98 B/op 3 allocs/op
|
||||
BenchmarkAce_GPlusAll 845606 1398 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAero_GPlusAll 1000000 1009 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBear_GPlusAll 103830 11386 ns/op 5488 B/op 61 allocs/op
|
||||
BenchmarkBeego_GPlusAll 82653 14784 ns/op 4576 B/op 39 allocs/op
|
||||
BenchmarkBone_GPlusAll 36601 33123 ns/op 11744 B/op 109 allocs/op
|
||||
BenchmarkChi_GPlusAll 95264 12831 ns/op 5616 B/op 39 allocs/op
|
||||
BenchmarkDenco_GPlusAll 567681 2950 ns/op 672 B/op 11 allocs/op
|
||||
BenchmarkEcho_GPlusAll 720366 1665 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGin_GPlusAll 1000000 1185 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_GPlusAll 71575 16365 ns/op 8040 B/op 103 allocs/op
|
||||
BenchmarkGoji_GPlusAll 136352 9191 ns/op 3696 B/op 22 allocs/op
|
||||
BenchmarkGojiv2_GPlusAll 38006 31802 ns/op 17616 B/op 154 allocs/op
|
||||
BenchmarkGoJsonRest_GPlusAll 57238 21561 ns/op 8117 B/op 170 allocs/op
|
||||
BenchmarkGoRestful_GPlusAll 15147 79276 ns/op 55520 B/op 192 allocs/op
|
||||
BenchmarkGorillaMux_GPlusAll 24446 48410 ns/op 16112 B/op 128 allocs/op
|
||||
BenchmarkGowwwRouter_GPlusAll 150112 7770 ns/op 4752 B/op 33 allocs/op
|
||||
BenchmarkHttpRouter_GPlusAll 1367820 878 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_GPlusAll 166628 8004 ns/op 4032 B/op 38 allocs/op
|
||||
BenchmarkKocha_GPlusAll 265694 4570 ns/op 976 B/op 43 allocs/op
|
||||
BenchmarkLARS_GPlusAll 1000000 1068 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_GPlusAll 54564 23305 ns/op 9568 B/op 104 allocs/op
|
||||
BenchmarkMartini_GPlusAll 16274 73845 ns/op 14016 B/op 145 allocs/op
|
||||
BenchmarkPat_GPlusAll 27181 44478 ns/op 15264 B/op 271 allocs/op
|
||||
BenchmarkPossum_GPlusAll 122587 10277 ns/op 5408 B/op 39 allocs/op
|
||||
BenchmarkR2router_GPlusAll 130137 9297 ns/op 5040 B/op 63 allocs/op
|
||||
BenchmarkRivet_GPlusAll 532438 3323 ns/op 768 B/op 11 allocs/op
|
||||
BenchmarkTango_GPlusAll 86054 14531 ns/op 3656 B/op 104 allocs/op
|
||||
BenchmarkTigerTonic_GPlusAll 33936 35356 ns/op 11600 B/op 242 allocs/op
|
||||
BenchmarkTraffic_GPlusAll 17833 68181 ns/op 26248 B/op 341 allocs/op
|
||||
BenchmarkVulcan_GPlusAll 120109 9861 ns/op 1274 B/op 39 allocs/op
|
||||
```
|
||||
|
||||
## Parse.com
|
||||
|
||||
```sh
|
||||
BenchmarkGin_ParseStatic 18877833 63.5 ns/op 0 B/op 0 allocs/op
|
||||
|
||||
BenchmarkAce_ParseStatic 19663731 60.8 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAero_ParseStatic 28967341 41.5 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBear_ParseStatic 3006984 402 ns/op 120 B/op 3 allocs/op
|
||||
BenchmarkBeego_ParseStatic 1000000 1031 ns/op 352 B/op 3 allocs/op
|
||||
BenchmarkBone_ParseStatic 1782482 675 ns/op 144 B/op 3 allocs/op
|
||||
BenchmarkChi_ParseStatic 1453261 819 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkDenco_ParseStatic 45023595 26.5 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkEcho_ParseStatic 17330470 69.3 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_ParseStatic 1644006 731 ns/op 296 B/op 5 allocs/op
|
||||
BenchmarkGoji_ParseStatic 7026930 170 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGojiv2_ParseStatic 517618 2037 ns/op 1312 B/op 10 allocs/op
|
||||
BenchmarkGoJsonRest_ParseStatic 1227080 975 ns/op 329 B/op 11 allocs/op
|
||||
BenchmarkGoRestful_ParseStatic 192458 6659 ns/op 4256 B/op 13 allocs/op
|
||||
BenchmarkGorillaMux_ParseStatic 744062 2109 ns/op 976 B/op 9 allocs/op
|
||||
BenchmarkGowwwRouter_ParseStatic 37781062 31.8 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpRouter_ParseStatic 45311223 26.5 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_ParseStatic 21383475 56.1 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkKocha_ParseStatic 29953290 40.1 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkLARS_ParseStatic 20036196 62.7 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_ParseStatic 1000000 1740 ns/op 736 B/op 8 allocs/op
|
||||
BenchmarkMartini_ParseStatic 404156 3801 ns/op 768 B/op 9 allocs/op
|
||||
BenchmarkPat_ParseStatic 1547180 772 ns/op 240 B/op 5 allocs/op
|
||||
BenchmarkPossum_ParseStatic 1608991 757 ns/op 416 B/op 3 allocs/op
|
||||
BenchmarkR2router_ParseStatic 3177936 385 ns/op 144 B/op 4 allocs/op
|
||||
BenchmarkRivet_ParseStatic 17783205 67.4 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkTango_ParseStatic 1210777 990 ns/op 248 B/op 8 allocs/op
|
||||
BenchmarkTigerTonic_ParseStatic 5316440 231 ns/op 48 B/op 1 allocs/op
|
||||
BenchmarkTraffic_ParseStatic 496050 2539 ns/op 1256 B/op 19 allocs/op
|
||||
BenchmarkVulcan_ParseStatic 2462798 488 ns/op 98 B/op 3 allocs/op
|
||||
BenchmarkAce_ParseParam 13393669 89.6 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAero_ParseParam 19836619 60.4 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBear_ParseParam 1405954 864 ns/op 467 B/op 5 allocs/op
|
||||
BenchmarkBeego_ParseParam 1000000 1065 ns/op 352 B/op 3 allocs/op
|
||||
BenchmarkBone_ParseParam 1000000 1698 ns/op 896 B/op 7 allocs/op
|
||||
BenchmarkChi_ParseParam 1356037 873 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkDenco_ParseParam 6241392 204 ns/op 64 B/op 1 allocs/op
|
||||
BenchmarkEcho_ParseParam 14088100 85.1 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGin_ParseParam 17426064 68.9 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_ParseParam 1000000 1254 ns/op 664 B/op 8 allocs/op
|
||||
BenchmarkGoji_ParseParam 1682574 713 ns/op 336 B/op 2 allocs/op
|
||||
BenchmarkGojiv2_ParseParam 502224 2333 ns/op 1360 B/op 12 allocs/op
|
||||
BenchmarkGoJsonRest_ParseParam 1000000 1401 ns/op 649 B/op 13 allocs/op
|
||||
BenchmarkGoRestful_ParseParam 182623 7097 ns/op 4576 B/op 14 allocs/op
|
||||
BenchmarkGorillaMux_ParseParam 482332 2477 ns/op 1280 B/op 10 allocs/op
|
||||
BenchmarkGowwwRouter_ParseParam 1834873 657 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkHttpRouter_ParseParam 23593393 51.0 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_ParseParam 2100160 574 ns/op 352 B/op 3 allocs/op
|
||||
BenchmarkKocha_ParseParam 4837220 252 ns/op 56 B/op 3 allocs/op
|
||||
BenchmarkLARS_ParseParam 18411192 66.2 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_ParseParam 571870 2398 ns/op 1072 B/op 10 allocs/op
|
||||
BenchmarkMartini_ParseParam 286262 4268 ns/op 1072 B/op 10 allocs/op
|
||||
BenchmarkPat_ParseParam 692906 2157 ns/op 992 B/op 15 allocs/op
|
||||
BenchmarkPossum_ParseParam 1000000 1011 ns/op 496 B/op 5 allocs/op
|
||||
BenchmarkR2router_ParseParam 1722735 697 ns/op 432 B/op 5 allocs/op
|
||||
BenchmarkRivet_ParseParam 6058054 203 ns/op 48 B/op 1 allocs/op
|
||||
BenchmarkTango_ParseParam 1000000 1061 ns/op 280 B/op 8 allocs/op
|
||||
BenchmarkTigerTonic_ParseParam 890275 2277 ns/op 784 B/op 15 allocs/op
|
||||
BenchmarkTraffic_ParseParam 351322 3543 ns/op 1896 B/op 21 allocs/op
|
||||
BenchmarkVulcan_ParseParam 2076544 572 ns/op 98 B/op 3 allocs/op
|
||||
BenchmarkAce_Parse2Params 11718074 101 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAero_Parse2Params 16264988 73.4 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBear_Parse2Params 1238322 973 ns/op 496 B/op 5 allocs/op
|
||||
BenchmarkBeego_Parse2Params 1000000 1120 ns/op 352 B/op 3 allocs/op
|
||||
BenchmarkBone_Parse2Params 1000000 1632 ns/op 848 B/op 6 allocs/op
|
||||
BenchmarkChi_Parse2Params 1239477 955 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkDenco_Parse2Params 4944133 245 ns/op 64 B/op 1 allocs/op
|
||||
BenchmarkEcho_Parse2Params 10518286 114 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGin_Parse2Params 14505195 82.7 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_Parse2Params 1000000 1437 ns/op 712 B/op 9 allocs/op
|
||||
BenchmarkGoji_Parse2Params 1689883 707 ns/op 336 B/op 2 allocs/op
|
||||
BenchmarkGojiv2_Parse2Params 502334 2308 ns/op 1344 B/op 11 allocs/op
|
||||
BenchmarkGoJsonRest_Parse2Params 1000000 1771 ns/op 713 B/op 14 allocs/op
|
||||
BenchmarkGoRestful_Parse2Params 159092 7583 ns/op 4928 B/op 14 allocs/op
|
||||
BenchmarkGorillaMux_Parse2Params 417548 2980 ns/op 1296 B/op 10 allocs/op
|
||||
BenchmarkGowwwRouter_Parse2Params 1751737 686 ns/op 432 B/op 3 allocs/op
|
||||
BenchmarkHttpRouter_Parse2Params 18089204 66.3 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_Parse2Params 1556986 777 ns/op 384 B/op 4 allocs/op
|
||||
BenchmarkKocha_Parse2Params 2493082 485 ns/op 128 B/op 5 allocs/op
|
||||
BenchmarkLARS_Parse2Params 15350108 78.5 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_Parse2Params 530974 2605 ns/op 1072 B/op 10 allocs/op
|
||||
BenchmarkMartini_Parse2Params 247069 4673 ns/op 1152 B/op 11 allocs/op
|
||||
BenchmarkPat_Parse2Params 816295 2126 ns/op 752 B/op 16 allocs/op
|
||||
BenchmarkPossum_Parse2Params 1000000 1002 ns/op 496 B/op 5 allocs/op
|
||||
BenchmarkR2router_Parse2Params 1569771 733 ns/op 432 B/op 5 allocs/op
|
||||
BenchmarkRivet_Parse2Params 4080546 295 ns/op 96 B/op 1 allocs/op
|
||||
BenchmarkTango_Parse2Params 1000000 1121 ns/op 312 B/op 8 allocs/op
|
||||
BenchmarkTigerTonic_Parse2Params 399556 3470 ns/op 1168 B/op 22 allocs/op
|
||||
BenchmarkTraffic_Parse2Params 314194 4159 ns/op 1944 B/op 22 allocs/op
|
||||
BenchmarkVulcan_Parse2Params 1827559 664 ns/op 98 B/op 3 allocs/op
|
||||
BenchmarkAce_ParseAll 478395 2503 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAero_ParseAll 715392 1658 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkBear_ParseAll 59191 20124 ns/op 8928 B/op 110 allocs/op
|
||||
BenchmarkBeego_ParseAll 45507 27266 ns/op 9152 B/op 78 allocs/op
|
||||
BenchmarkBone_ParseAll 29328 41459 ns/op 16208 B/op 147 allocs/op
|
||||
BenchmarkChi_ParseAll 48531 25053 ns/op 11232 B/op 78 allocs/op
|
||||
BenchmarkDenco_ParseAll 325532 4284 ns/op 928 B/op 16 allocs/op
|
||||
BenchmarkEcho_ParseAll 433771 2759 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGin_ParseAll 576316 2082 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGocraftWeb_ParseAll 41500 29692 ns/op 13728 B/op 181 allocs/op
|
||||
BenchmarkGoji_ParseAll 80833 15563 ns/op 5376 B/op 32 allocs/op
|
||||
BenchmarkGojiv2_ParseAll 19836 60335 ns/op 34448 B/op 277 allocs/op
|
||||
BenchmarkGoJsonRest_ParseAll 32210 38027 ns/op 13866 B/op 321 allocs/op
|
||||
BenchmarkGoRestful_ParseAll 6644 190842 ns/op 117600 B/op 354 allocs/op
|
||||
BenchmarkGorillaMux_ParseAll 12634 95894 ns/op 30288 B/op 250 allocs/op
|
||||
BenchmarkGowwwRouter_ParseAll 98152 12159 ns/op 6912 B/op 48 allocs/op
|
||||
BenchmarkHttpRouter_ParseAll 933208 1273 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHttpTreeMux_ParseAll 107191 11554 ns/op 5728 B/op 51 allocs/op
|
||||
BenchmarkKocha_ParseAll 184862 6225 ns/op 1112 B/op 54 allocs/op
|
||||
BenchmarkLARS_ParseAll 644546 1858 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkMacaron_ParseAll 26145 46484 ns/op 19136 B/op 208 allocs/op
|
||||
BenchmarkMartini_ParseAll 10000 121838 ns/op 25072 B/op 253 allocs/op
|
||||
BenchmarkPat_ParseAll 25417 47196 ns/op 15216 B/op 308 allocs/op
|
||||
BenchmarkPossum_ParseAll 58550 20735 ns/op 10816 B/op 78 allocs/op
|
||||
BenchmarkR2router_ParseAll 72732 16584 ns/op 8352 B/op 120 allocs/op
|
||||
BenchmarkRivet_ParseAll 281365 4968 ns/op 912 B/op 16 allocs/op
|
||||
BenchmarkTango_ParseAll 42831 28668 ns/op 7168 B/op 208 allocs/op
|
||||
BenchmarkTigerTonic_ParseAll 23774 49972 ns/op 16048 B/op 332 allocs/op
|
||||
BenchmarkTraffic_ParseAll 10000 104679 ns/op 45520 B/op 605 allocs/op
|
||||
BenchmarkVulcan_ParseAll 64810 18108 ns/op 2548 B/op 78 allocs/op
|
||||
```
|
513
CHANGELOG.md
513
CHANGELOG.md
|
@ -1,513 +0,0 @@
|
|||
# Gin ChangeLog
|
||||
|
||||
## Gin v1.8.2
|
||||
|
||||
### Bugs
|
||||
|
||||
* fix(route): redirectSlash bug ([#3227]((https://github.com/gin-gonic/gin/pull/3227)))
|
||||
* fix(engine): missing route params for CreateTestContext ([#2778]((https://github.com/gin-gonic/gin/pull/2778))) ([#2803]((https://github.com/gin-gonic/gin/pull/2803)))
|
||||
|
||||
### Security
|
||||
|
||||
* Fix the GO-2022-1144 vulnerability ([#3432]((https://github.com/gin-gonic/gin/pull/3432)))
|
||||
|
||||
## Gin v1.8.1
|
||||
|
||||
### ENHANCEMENTS
|
||||
|
||||
* feat(context): add ContextWithFallback feature flag [#3172](https://git.internal/re/gin/pull/3172)
|
||||
|
||||
## Gin v1.8.0
|
||||
|
||||
## Break Changes
|
||||
|
||||
* TrustedProxies: Add default IPv6 support and refactor [#2967](https://git.internal/re/gin/pull/2967). Please replace `RemoteIP() (net.IP, bool)` with `RemoteIP() net.IP`
|
||||
* gin.Context with fallback value from gin.Context.Request.Context() [#2751](https://git.internal/re/gin/pull/2751)
|
||||
|
||||
### BUGFIXES
|
||||
|
||||
* Fixed SetOutput() panics on go 1.17 [#2861](https://git.internal/re/gin/pull/2861)
|
||||
* Fix: wrong when wildcard follows named param [#2983](https://git.internal/re/gin/pull/2983)
|
||||
* Fix: missing sameSite when do context.reset() [#3123](https://git.internal/re/gin/pull/3123)
|
||||
|
||||
### ENHANCEMENTS
|
||||
|
||||
* Use Header() instead of deprecated HeaderMap [#2694](https://git.internal/re/gin/pull/2694)
|
||||
* RouterGroup.Handle regular match optimization of http method [#2685](https://git.internal/re/gin/pull/2685)
|
||||
* Add support go-json, another drop-in json replacement [#2680](https://git.internal/re/gin/pull/2680)
|
||||
* Use errors.New to replace fmt.Errorf will much better [#2707](https://git.internal/re/gin/pull/2707)
|
||||
* Use Duration.Truncate for truncating precision [#2711](https://git.internal/re/gin/pull/2711)
|
||||
* Get client IP when using Cloudflare [#2723](https://git.internal/re/gin/pull/2723)
|
||||
* Optimize code adjust [#2700](https://git.internal/re/gin/pull/2700/files)
|
||||
* Optimize code and reduce code cyclomatic complexity [#2737](https://git.internal/re/gin/pull/2737)
|
||||
* Improve sliceValidateError.Error performance [#2765](https://git.internal/re/gin/pull/2765)
|
||||
* Support custom struct tag [#2720](https://git.internal/re/gin/pull/2720)
|
||||
* Improve router group tests [#2787](https://git.internal/re/gin/pull/2787)
|
||||
* Fallback Context.Deadline() Context.Done() Context.Err() to Context.Request.Context() [#2769](https://git.internal/re/gin/pull/2769)
|
||||
* Some codes optimize [#2830](https://git.internal/re/gin/pull/2830) [#2834](https://git.internal/re/gin/pull/2834) [#2838](https://git.internal/re/gin/pull/2838) [#2837](https://git.internal/re/gin/pull/2837) [#2788](https://git.internal/re/gin/pull/2788) [#2848](https://git.internal/re/gin/pull/2848) [#2851](https://git.internal/re/gin/pull/2851) [#2701](https://git.internal/re/gin/pull/2701)
|
||||
* TrustedProxies: Add default IPv6 support and refactor [#2967](https://git.internal/re/gin/pull/2967)
|
||||
* Test(route): expose performRequest func [#3012](https://git.internal/re/gin/pull/3012)
|
||||
* Support h2c with prior knowledge [#1398](https://git.internal/re/gin/pull/1398)
|
||||
* Feat attachment filename support utf8 [#3071](https://git.internal/re/gin/pull/3071)
|
||||
* Feat: add StaticFileFS [#2749](https://git.internal/re/gin/pull/2749)
|
||||
* Feat(context): return GIN Context from Value method [#2825](https://git.internal/re/gin/pull/2825)
|
||||
* Feat: automatically SetMode to TestMode when run go test [#3139](https://git.internal/re/gin/pull/3139)
|
||||
* Add TOML bining for gin [#3081](https://git.internal/re/gin/pull/3081)
|
||||
* IPv6 add default trusted proxies [#3033](https://git.internal/re/gin/pull/3033)
|
||||
|
||||
### DOCS
|
||||
|
||||
* Add note about nomsgpack tag to the readme [#2703](https://git.internal/re/gin/pull/2703)
|
||||
|
||||
## Gin v1.7.7
|
||||
|
||||
### BUGFIXES
|
||||
|
||||
* Fixed X-Forwarded-For unsafe handling of CVE-2020-28483 [#2844](https://git.internal/re/gin/pull/2844), closed issue [#2862](https://git.internal/re/gin/issues/2862).
|
||||
* Tree: updated the code logic for `latestNode` [#2897](https://git.internal/re/gin/pull/2897), closed issue [#2894](https://git.internal/re/gin/issues/2894) [#2878](https://git.internal/re/gin/issues/2878).
|
||||
* Tree: fixed the misplacement of adding slashes [#2847](https://git.internal/re/gin/pull/2847), closed issue [#2843](https://git.internal/re/gin/issues/2843).
|
||||
* Tree: fixed tsr with mixed static and wildcard paths [#2924](https://git.internal/re/gin/pull/2924), closed issue [#2918](https://git.internal/re/gin/issues/2918).
|
||||
|
||||
### ENHANCEMENTS
|
||||
|
||||
* TrustedProxies: make it backward-compatible [#2887](https://git.internal/re/gin/pull/2887), closed issue [#2819](https://git.internal/re/gin/issues/2819).
|
||||
* TrustedPlatform: provide custom options for another CDN services [#2906](https://git.internal/re/gin/pull/2906).
|
||||
|
||||
### DOCS
|
||||
|
||||
* NoMethod: added usage annotation ([#2832](https://git.internal/re/gin/pull/2832#issuecomment-929954463)).
|
||||
|
||||
## Gin v1.7.6
|
||||
|
||||
### BUGFIXES
|
||||
|
||||
* bump new release to fix v1.7.5 release error by using v1.7.4 codes.
|
||||
|
||||
## Gin v1.7.4
|
||||
|
||||
### BUGFIXES
|
||||
|
||||
* bump new release to fix checksum mismatch
|
||||
|
||||
## Gin v1.7.3
|
||||
|
||||
### BUGFIXES
|
||||
|
||||
* fix level 1 router match [#2767](https://git.internal/re/gin/issues/2767), [#2796](https://git.internal/re/gin/issues/2796)
|
||||
|
||||
## Gin v1.7.2
|
||||
|
||||
### BUGFIXES
|
||||
|
||||
* Fix conflict between param and exact path [#2706](https://git.internal/re/gin/issues/2706). Close issue [#2682](https://git.internal/re/gin/issues/2682) [#2696](https://git.internal/re/gin/issues/2696).
|
||||
|
||||
## Gin v1.7.1
|
||||
|
||||
### BUGFIXES
|
||||
|
||||
* fix: data race with trustedCIDRs from [#2674](https://git.internal/re/gin/issues/2674)([#2675](https://git.internal/re/gin/pull/2675))
|
||||
|
||||
## Gin v1.7.0
|
||||
|
||||
### BUGFIXES
|
||||
|
||||
* fix compile error from [#2572](https://git.internal/re/gin/pull/2572) ([#2600](https://git.internal/re/gin/pull/2600))
|
||||
* fix: print headers without Authorization header on broken pipe ([#2528](https://git.internal/re/gin/pull/2528))
|
||||
* fix(tree): reassign fullpath when register new node ([#2366](https://git.internal/re/gin/pull/2366))
|
||||
|
||||
### ENHANCEMENTS
|
||||
|
||||
* Support params and exact routes without creating conflicts ([#2663](https://git.internal/re/gin/pull/2663))
|
||||
* chore: improve render string performance ([#2365](https://git.internal/re/gin/pull/2365))
|
||||
* Sync route tree to httprouter latest code ([#2368](https://git.internal/re/gin/pull/2368))
|
||||
* chore: rename getQueryCache/getFormCache to initQueryCache/initFormCa ([#2375](https://git.internal/re/gin/pull/2375))
|
||||
* chore(performance): improve countParams ([#2378](https://git.internal/re/gin/pull/2378))
|
||||
* Remove some functions that have the same effect as the bytes package ([#2387](https://git.internal/re/gin/pull/2387))
|
||||
* update:SetMode function ([#2321](https://git.internal/re/gin/pull/2321))
|
||||
* remove an unused type SecureJSONPrefix ([#2391](https://git.internal/re/gin/pull/2391))
|
||||
* Add a redirect sample for POST method ([#2389](https://git.internal/re/gin/pull/2389))
|
||||
* Add CustomRecovery builtin middleware ([#2322](https://git.internal/re/gin/pull/2322))
|
||||
* binding: avoid 2038 problem on 32-bit architectures ([#2450](https://git.internal/re/gin/pull/2450))
|
||||
* Prevent panic in Context.GetQuery() when there is no Request ([#2412](https://git.internal/re/gin/pull/2412))
|
||||
* Add GetUint and GetUint64 method on gin.context ([#2487](https://git.internal/re/gin/pull/2487))
|
||||
* update content-disposition header to MIME-style ([#2512](https://git.internal/re/gin/pull/2512))
|
||||
* reduce allocs and improve the render `WriteString` ([#2508](https://git.internal/re/gin/pull/2508))
|
||||
* implement ".Unwrap() error" on Error type ([#2525](https://git.internal/re/gin/pull/2525)) ([#2526](https://git.internal/re/gin/pull/2526))
|
||||
* Allow bind with a map[string]string ([#2484](https://git.internal/re/gin/pull/2484))
|
||||
* chore: update tree ([#2371](https://git.internal/re/gin/pull/2371))
|
||||
* Support binding for slice/array obj [Rewrite] ([#2302](https://git.internal/re/gin/pull/2302))
|
||||
* basic auth: fix timing oracle ([#2609](https://git.internal/re/gin/pull/2609))
|
||||
* Add mixed param and non-param paths (port of httprouter[#329](https://git.internal/re/gin/pull/329)) ([#2663](https://git.internal/re/gin/pull/2663))
|
||||
* feat(engine): add trustedproxies and remoteIP ([#2632](https://git.internal/re/gin/pull/2632))
|
||||
|
||||
## Gin v1.6.3
|
||||
|
||||
### ENHANCEMENTS
|
||||
|
||||
* Improve performance: Change `*sync.RWMutex` to `sync.RWMutex` in context. [#2351](https://git.internal/re/gin/pull/2351)
|
||||
|
||||
## Gin v1.6.2
|
||||
|
||||
### BUGFIXES
|
||||
* fix missing initial sync.RWMutex [#2305](https://git.internal/re/gin/pull/2305)
|
||||
### ENHANCEMENTS
|
||||
* Add set samesite in cookie. [#2306](https://git.internal/re/gin/pull/2306)
|
||||
|
||||
## Gin v1.6.1
|
||||
|
||||
### BUGFIXES
|
||||
* Revert "fix accept incoming network connections" [#2294](https://git.internal/re/gin/pull/2294)
|
||||
|
||||
## Gin v1.6.0
|
||||
|
||||
### BREAKING
|
||||
* chore(performance): Improve performance for adding RemoveExtraSlash flag [#2159](https://git.internal/re/gin/pull/2159)
|
||||
* drop support govendor [#2148](https://git.internal/re/gin/pull/2148)
|
||||
* Added support for SameSite cookie flag [#1615](https://git.internal/re/gin/pull/1615)
|
||||
### FEATURES
|
||||
* add yaml negotiation [#2220](https://git.internal/re/gin/pull/2220)
|
||||
* FileFromFS [#2112](https://git.internal/re/gin/pull/2112)
|
||||
### BUGFIXES
|
||||
* Unix Socket Handling [#2280](https://git.internal/re/gin/pull/2280)
|
||||
* Use json marshall in context json to fix breaking new line issue. Fixes #2209 [#2228](https://git.internal/re/gin/pull/2228)
|
||||
* fix accept incoming network connections [#2216](https://git.internal/re/gin/pull/2216)
|
||||
* Fixed a bug in the calculation of the maximum number of parameters [#2166](https://git.internal/re/gin/pull/2166)
|
||||
* [FIX] allow empty headers on DataFromReader [#2121](https://git.internal/re/gin/pull/2121)
|
||||
* Add mutex for protect Context.Keys map [#1391](https://git.internal/re/gin/pull/1391)
|
||||
### ENHANCEMENTS
|
||||
* Add mitigation for log injection [#2277](https://git.internal/re/gin/pull/2277)
|
||||
* tree: range over nodes values [#2229](https://git.internal/re/gin/pull/2229)
|
||||
* tree: remove duplicate assignment [#2222](https://git.internal/re/gin/pull/2222)
|
||||
* chore: upgrade go-isatty and json-iterator/go [#2215](https://git.internal/re/gin/pull/2215)
|
||||
* path: sync code with httprouter [#2212](https://git.internal/re/gin/pull/2212)
|
||||
* Use zero-copy approach to convert types between string and byte slice [#2206](https://git.internal/re/gin/pull/2206)
|
||||
* Reuse bytes when cleaning the URL paths [#2179](https://git.internal/re/gin/pull/2179)
|
||||
* tree: remove one else statement [#2177](https://git.internal/re/gin/pull/2177)
|
||||
* tree: sync httprouter update (#2173) (#2172) [#2171](https://git.internal/re/gin/pull/2171)
|
||||
* tree: sync part httprouter codes and reduce if/else [#2163](https://git.internal/re/gin/pull/2163)
|
||||
* use http method constant [#2155](https://git.internal/re/gin/pull/2155)
|
||||
* upgrade go-validator to v10 [#2149](https://git.internal/re/gin/pull/2149)
|
||||
* Refactor redirect request in gin.go [#1970](https://git.internal/re/gin/pull/1970)
|
||||
* Add build tag nomsgpack [#1852](https://git.internal/re/gin/pull/1852)
|
||||
### DOCS
|
||||
* docs(path): improve comments [#2223](https://git.internal/re/gin/pull/2223)
|
||||
* Renew README to fit the modification of SetCookie method [#2217](https://git.internal/re/gin/pull/2217)
|
||||
* Fix spelling [#2202](https://git.internal/re/gin/pull/2202)
|
||||
* Remove broken link from README. [#2198](https://git.internal/re/gin/pull/2198)
|
||||
* Update docs on Context.Done(), Context.Deadline() and Context.Err() [#2196](https://git.internal/re/gin/pull/2196)
|
||||
* Update validator to v10 [#2190](https://git.internal/re/gin/pull/2190)
|
||||
* upgrade go-validator to v10 for README [#2189](https://git.internal/re/gin/pull/2189)
|
||||
* Update to currently output [#2188](https://git.internal/re/gin/pull/2188)
|
||||
* Fix "Custom Validators" example [#2186](https://git.internal/re/gin/pull/2186)
|
||||
* Add project to README [#2165](https://git.internal/re/gin/pull/2165)
|
||||
* docs(benchmarks): for gin v1.5 [#2153](https://git.internal/re/gin/pull/2153)
|
||||
* Changed wording for clarity in README.md [#2122](https://git.internal/re/gin/pull/2122)
|
||||
### MISC
|
||||
* ci support go1.14 [#2262](https://git.internal/re/gin/pull/2262)
|
||||
* chore: upgrade depend version [#2231](https://git.internal/re/gin/pull/2231)
|
||||
* Drop support go1.10 [#2147](https://git.internal/re/gin/pull/2147)
|
||||
* fix comment in `mode.go` [#2129](https://git.internal/re/gin/pull/2129)
|
||||
|
||||
## Gin v1.5.0
|
||||
|
||||
- [FIX] Use DefaultWriter and DefaultErrorWriter for debug messages [#1891](https://git.internal/re/gin/pull/1891)
|
||||
- [NEW] Now you can parse the inline lowercase start structure [#1893](https://git.internal/re/gin/pull/1893)
|
||||
- [FIX] Some code improvements [#1909](https://git.internal/re/gin/pull/1909)
|
||||
- [FIX] Use encode replace json marshal increase json encoder speed [#1546](https://git.internal/re/gin/pull/1546)
|
||||
- [NEW] Hold matched route full path in the Context [#1826](https://git.internal/re/gin/pull/1826)
|
||||
- [FIX] Fix context.Params race condition on Copy() [#1841](https://git.internal/re/gin/pull/1841)
|
||||
- [NEW] Add context param query cache [#1450](https://git.internal/re/gin/pull/1450)
|
||||
- [FIX] Improve GetQueryMap performance [#1918](https://git.internal/re/gin/pull/1918)
|
||||
- [FIX] Improve get post data [#1920](https://git.internal/re/gin/pull/1920)
|
||||
- [FIX] Use context instead of x/net/context [#1922](https://git.internal/re/gin/pull/1922)
|
||||
- [FIX] Attempt to fix PostForm cache bug [#1931](https://git.internal/re/gin/pull/1931)
|
||||
- [NEW] Add support of multipart multi files [#1949](https://git.internal/re/gin/pull/1949)
|
||||
- [NEW] Support bind http header param [#1957](https://git.internal/re/gin/pull/1957)
|
||||
- [FIX] Drop support for go1.8 and go1.9 [#1933](https://git.internal/re/gin/pull/1933)
|
||||
- [FIX] Bugfix for the FullPath feature [#1919](https://git.internal/re/gin/pull/1919)
|
||||
- [FIX] Gin1.5 bytes.Buffer to strings.Builder [#1939](https://git.internal/re/gin/pull/1939)
|
||||
- [FIX] Upgrade github.com/ugorji/go/codec [#1969](https://git.internal/re/gin/pull/1969)
|
||||
- [NEW] Support bind unix time [#1980](https://git.internal/re/gin/pull/1980)
|
||||
- [FIX] Simplify code [#2004](https://git.internal/re/gin/pull/2004)
|
||||
- [NEW] Support negative Content-Length in DataFromReader [#1981](https://git.internal/re/gin/pull/1981)
|
||||
- [FIX] Identify terminal on a RISC-V architecture for auto-colored logs [#2019](https://git.internal/re/gin/pull/2019)
|
||||
- [BREAKING] `Context.JSONP()` now expects a semicolon (`;`) at the end [#2007](https://git.internal/re/gin/pull/2007)
|
||||
- [BREAKING] Upgrade default `binding.Validator` to v9 (see [its changelog](https://github.com/go-playground/validator/releases/tag/v9.0.0)) [#1015](https://git.internal/re/gin/pull/1015)
|
||||
- [NEW] Add `DisallowUnknownFields()` in `Context.BindJSON()` [#2028](https://git.internal/re/gin/pull/2028)
|
||||
- [NEW] Use specific `net.Listener` with `Engine.RunListener()` [#2023](https://git.internal/re/gin/pull/2023)
|
||||
- [FIX] Fix some typo [#2079](https://git.internal/re/gin/pull/2079) [#2080](https://git.internal/re/gin/pull/2080)
|
||||
- [FIX] Relocate binding body tests [#2086](https://git.internal/re/gin/pull/2086)
|
||||
- [FIX] Use Writer in Context.Status [#1606](https://git.internal/re/gin/pull/1606)
|
||||
- [FIX] `Engine.RunUnix()` now returns the error if it can't change the file mode [#2093](https://git.internal/re/gin/pull/2093)
|
||||
- [FIX] `RouterGroup.StaticFS()` leaked files. Now it closes them. [#2118](https://git.internal/re/gin/pull/2118)
|
||||
- [FIX] `Context.Request.FormFile` leaked file. Now it closes it. [#2114](https://git.internal/re/gin/pull/2114)
|
||||
- [FIX] Ignore walking on `form:"-"` mapping [#1943](https://git.internal/re/gin/pull/1943)
|
||||
|
||||
### Gin v1.4.0
|
||||
|
||||
- [NEW] Support for [Go Modules](https://github.com/golang/go/wiki/Modules) [#1569](https://git.internal/re/gin/pull/1569)
|
||||
- [NEW] Refactor of form mapping multipart request [#1829](https://git.internal/re/gin/pull/1829)
|
||||
- [FIX] Truncate Latency precision in long running request [#1830](https://git.internal/re/gin/pull/1830)
|
||||
- [FIX] IsTerm flag should not be affected by DisableConsoleColor method. [#1802](https://git.internal/re/gin/pull/1802)
|
||||
- [NEW] Supporting file binding [#1264](https://git.internal/re/gin/pull/1264)
|
||||
- [NEW] Add support for mapping arrays [#1797](https://git.internal/re/gin/pull/1797)
|
||||
- [FIX] Readme updates [#1793](https://git.internal/re/gin/pull/1793) [#1788](https://git.internal/re/gin/pull/1788) [1789](https://git.internal/re/gin/pull/1789)
|
||||
- [FIX] StaticFS: Fixed Logging two log lines on 404. [#1805](https://git.internal/re/gin/pull/1805), [#1804](https://git.internal/re/gin/pull/1804)
|
||||
- [NEW] Make context.Keys available as LogFormatterParams [#1779](https://git.internal/re/gin/pull/1779)
|
||||
- [NEW] Use internal/json for Marshal/Unmarshal [#1791](https://git.internal/re/gin/pull/1791)
|
||||
- [NEW] Support mapping time.Duration [#1794](https://git.internal/re/gin/pull/1794)
|
||||
- [NEW] Refactor form mappings [#1749](https://git.internal/re/gin/pull/1749)
|
||||
- [NEW] Added flag to context.Stream indicates if client disconnected in middle of stream [#1252](https://git.internal/re/gin/pull/1252)
|
||||
- [FIX] Moved [examples](https://github.com/gin-gonic/examples) to stand alone Repo [#1775](https://git.internal/re/gin/pull/1775)
|
||||
- [NEW] Extend context.File to allow for the content-disposition attachments via a new method context.Attachment [#1260](https://git.internal/re/gin/pull/1260)
|
||||
- [FIX] Support HTTP content negotiation wildcards [#1112](https://git.internal/re/gin/pull/1112)
|
||||
- [NEW] Add prefix from X-Forwarded-Prefix in redirectTrailingSlash [#1238](https://git.internal/re/gin/pull/1238)
|
||||
- [FIX] context.Copy() race condition [#1020](https://git.internal/re/gin/pull/1020)
|
||||
- [NEW] Add context.HandlerNames() [#1729](https://git.internal/re/gin/pull/1729)
|
||||
- [FIX] Change color methods to public in the defaultLogger. [#1771](https://git.internal/re/gin/pull/1771)
|
||||
- [FIX] Update writeHeaders method to use http.Header.Set [#1722](https://git.internal/re/gin/pull/1722)
|
||||
- [NEW] Add response size to LogFormatterParams [#1752](https://git.internal/re/gin/pull/1752)
|
||||
- [NEW] Allow ignoring field on form mapping [#1733](https://git.internal/re/gin/pull/1733)
|
||||
- [NEW] Add a function to force color in console output. [#1724](https://git.internal/re/gin/pull/1724)
|
||||
- [FIX] Context.Next() - recheck len of handlers on every iteration. [#1745](https://git.internal/re/gin/pull/1745)
|
||||
- [FIX] Fix all errcheck warnings [#1739](https://git.internal/re/gin/pull/1739) [#1653](https://git.internal/re/gin/pull/1653)
|
||||
- [NEW] context: inherits context cancellation and deadline from http.Request context for Go>=1.7 [#1690](https://git.internal/re/gin/pull/1690)
|
||||
- [NEW] Binding for URL Params [#1694](https://git.internal/re/gin/pull/1694)
|
||||
- [NEW] Add LoggerWithFormatter method [#1677](https://git.internal/re/gin/pull/1677)
|
||||
- [FIX] CI testing updates [#1671](https://git.internal/re/gin/pull/1671) [#1670](https://git.internal/re/gin/pull/1670) [#1682](https://git.internal/re/gin/pull/1682) [#1669](https://git.internal/re/gin/pull/1669)
|
||||
- [FIX] StaticFS(): Send 404 when path does not exist [#1663](https://git.internal/re/gin/pull/1663)
|
||||
- [FIX] Handle nil body for JSON binding [#1638](https://git.internal/re/gin/pull/1638)
|
||||
- [FIX] Support bind uri param [#1612](https://git.internal/re/gin/pull/1612)
|
||||
- [FIX] recovery: fix issue with syscall import on google app engine [#1640](https://git.internal/re/gin/pull/1640)
|
||||
- [FIX] Make sure the debug log contains line breaks [#1650](https://git.internal/re/gin/pull/1650)
|
||||
- [FIX] Panic stack trace being printed during recovery of broken pipe [#1089](https://git.internal/re/gin/pull/1089) [#1259](https://git.internal/re/gin/pull/1259)
|
||||
- [NEW] RunFd method to run http.Server through a file descriptor [#1609](https://git.internal/re/gin/pull/1609)
|
||||
- [NEW] Yaml binding support [#1618](https://git.internal/re/gin/pull/1618)
|
||||
- [FIX] Pass MaxMultipartMemory when FormFile is called [#1600](https://git.internal/re/gin/pull/1600)
|
||||
- [FIX] LoadHTML* tests [#1559](https://git.internal/re/gin/pull/1559)
|
||||
- [FIX] Removed use of sync.pool from HandleContext [#1565](https://git.internal/re/gin/pull/1565)
|
||||
- [FIX] Format output log to os.Stderr [#1571](https://git.internal/re/gin/pull/1571)
|
||||
- [FIX] Make logger use a yellow background and a darkgray text for legibility [#1570](https://git.internal/re/gin/pull/1570)
|
||||
- [FIX] Remove sensitive request information from panic log. [#1370](https://git.internal/re/gin/pull/1370)
|
||||
- [FIX] log.Println() does not print timestamp [#829](https://git.internal/re/gin/pull/829) [#1560](https://git.internal/re/gin/pull/1560)
|
||||
- [NEW] Add PureJSON renderer [#694](https://git.internal/re/gin/pull/694)
|
||||
- [FIX] Add missing copyright and update if/else [#1497](https://git.internal/re/gin/pull/1497)
|
||||
- [FIX] Update msgpack usage [#1498](https://git.internal/re/gin/pull/1498)
|
||||
- [FIX] Use protobuf on render [#1496](https://git.internal/re/gin/pull/1496)
|
||||
- [FIX] Add support for Protobuf format response [#1479](https://git.internal/re/gin/pull/1479)
|
||||
- [NEW] Set default time format in form binding [#1487](https://git.internal/re/gin/pull/1487)
|
||||
- [FIX] Add BindXML and ShouldBindXML [#1485](https://git.internal/re/gin/pull/1485)
|
||||
- [NEW] Upgrade dependency libraries [#1491](https://git.internal/re/gin/pull/1491)
|
||||
|
||||
|
||||
## Gin v1.3.0
|
||||
|
||||
- [NEW] Add [`func (*Context) QueryMap`](https://godoc.org/git.internal/re/gin#Context.QueryMap), [`func (*Context) GetQueryMap`](https://godoc.org/git.internal/re/gin#Context.GetQueryMap), [`func (*Context) PostFormMap`](https://godoc.org/git.internal/re/gin#Context.PostFormMap) and [`func (*Context) GetPostFormMap`](https://godoc.org/git.internal/re/gin#Context.GetPostFormMap) to support `type map[string]string` as query string or form parameters, see [#1383](https://git.internal/re/gin/pull/1383)
|
||||
- [NEW] Add [`func (*Context) AsciiJSON`](https://godoc.org/git.internal/re/gin#Context.AsciiJSON), see [#1358](https://git.internal/re/gin/pull/1358)
|
||||
- [NEW] Add `Pusher()` in [`type ResponseWriter`](https://godoc.org/git.internal/re/gin#ResponseWriter) for supporting http2 push, see [#1273](https://git.internal/re/gin/pull/1273)
|
||||
- [NEW] Add [`func (*Context) DataFromReader`](https://godoc.org/git.internal/re/gin#Context.DataFromReader) for serving dynamic data, see [#1304](https://git.internal/re/gin/pull/1304)
|
||||
- [NEW] Add [`func (*Context) ShouldBindBodyWith`](https://godoc.org/git.internal/re/gin#Context.ShouldBindBodyWith) allowing to call binding multiple times, see [#1341](https://git.internal/re/gin/pull/1341)
|
||||
- [NEW] Support pointers in form binding, see [#1336](https://git.internal/re/gin/pull/1336)
|
||||
- [NEW] Add [`func (*Context) JSONP`](https://godoc.org/git.internal/re/gin#Context.JSONP), see [#1333](https://git.internal/re/gin/pull/1333)
|
||||
- [NEW] Support default value in form binding, see [#1138](https://git.internal/re/gin/pull/1138)
|
||||
- [NEW] Expose validator engine in [`type StructValidator`](https://godoc.org/git.internal/re/gin/binding#StructValidator), see [#1277](https://git.internal/re/gin/pull/1277)
|
||||
- [NEW] Add [`func (*Context) ShouldBind`](https://godoc.org/git.internal/re/gin#Context.ShouldBind), [`func (*Context) ShouldBindQuery`](https://godoc.org/git.internal/re/gin#Context.ShouldBindQuery) and [`func (*Context) ShouldBindJSON`](https://godoc.org/git.internal/re/gin#Context.ShouldBindJSON), see [#1047](https://git.internal/re/gin/pull/1047)
|
||||
- [NEW] Add support for `time.Time` location in form binding, see [#1117](https://git.internal/re/gin/pull/1117)
|
||||
- [NEW] Add [`func (*Context) BindQuery`](https://godoc.org/git.internal/re/gin#Context.BindQuery), see [#1029](https://git.internal/re/gin/pull/1029)
|
||||
- [NEW] Make [jsonite](https://github.com/json-iterator/go) optional with build tags, see [#1026](https://git.internal/re/gin/pull/1026)
|
||||
- [NEW] Show query string in logger, see [#999](https://git.internal/re/gin/pull/999)
|
||||
- [NEW] Add [`func (*Context) SecureJSON`](https://godoc.org/git.internal/re/gin#Context.SecureJSON), see [#987](https://git.internal/re/gin/pull/987) and [#993](https://git.internal/re/gin/pull/993)
|
||||
- [DEPRECATE] `func (*Context) GetCookie` for [`func (*Context) Cookie`](https://godoc.org/git.internal/re/gin#Context.Cookie)
|
||||
- [FIX] Don't display color tags if [`func DisableConsoleColor`](https://godoc.org/git.internal/re/gin#DisableConsoleColor) called, see [#1072](https://git.internal/re/gin/pull/1072)
|
||||
- [FIX] Gin Mode `""` when calling [`func Mode`](https://godoc.org/git.internal/re/gin#Mode) now returns `const DebugMode`, see [#1250](https://git.internal/re/gin/pull/1250)
|
||||
- [FIX] `Flush()` now doesn't overwrite `responseWriter` status code, see [#1460](https://git.internal/re/gin/pull/1460)
|
||||
|
||||
## Gin 1.2.0
|
||||
|
||||
- [NEW] Switch from godeps to govendor
|
||||
- [NEW] Add support for Let's Encrypt via gin-gonic/autotls
|
||||
- [NEW] Improve README examples and add extra at examples folder
|
||||
- [NEW] Improved support with App Engine
|
||||
- [NEW] Add custom template delimiters, see #860
|
||||
- [NEW] Add Template Func Maps, see #962
|
||||
- [NEW] Add \*context.Handler(), see #928
|
||||
- [NEW] Add \*context.GetRawData()
|
||||
- [NEW] Add \*context.GetHeader() (request)
|
||||
- [NEW] Add \*context.AbortWithStatusJSON() (JSON content type)
|
||||
- [NEW] Add \*context.Keys type cast helpers
|
||||
- [NEW] Add \*context.ShouldBindWith()
|
||||
- [NEW] Add \*context.MustBindWith()
|
||||
- [NEW] Add \*engine.SetFuncMap()
|
||||
- [DEPRECATE] On next release: \*context.BindWith(), see #855
|
||||
- [FIX] Refactor render
|
||||
- [FIX] Reworked tests
|
||||
- [FIX] logger now supports cygwin
|
||||
- [FIX] Use X-Forwarded-For before X-Real-Ip
|
||||
- [FIX] time.Time binding (#904)
|
||||
|
||||
## Gin 1.1.4
|
||||
|
||||
- [NEW] Support google appengine for IsTerminal func
|
||||
|
||||
## Gin 1.1.3
|
||||
|
||||
- [FIX] Reverted Logger: skip ANSI color commands
|
||||
|
||||
## Gin 1.1
|
||||
|
||||
- [NEW] Implement QueryArray and PostArray methods
|
||||
- [NEW] Refactor GetQuery and GetPostForm
|
||||
- [NEW] Add contribution guide
|
||||
- [FIX] Corrected typos in README
|
||||
- [FIX] Removed additional Iota
|
||||
- [FIX] Changed imports to gopkg instead of github in README (#733)
|
||||
- [FIX] Logger: skip ANSI color commands if output is not a tty
|
||||
|
||||
## Gin 1.0rc2 (...)
|
||||
|
||||
- [PERFORMANCE] Fast path for writing Content-Type.
|
||||
- [PERFORMANCE] Much faster 404 routing
|
||||
- [PERFORMANCE] Allocation optimizations
|
||||
- [PERFORMANCE] Faster root tree lookup
|
||||
- [PERFORMANCE] Zero overhead, String() and JSON() rendering.
|
||||
- [PERFORMANCE] Faster ClientIP parsing
|
||||
- [PERFORMANCE] Much faster SSE implementation
|
||||
- [NEW] Benchmarks suite
|
||||
- [NEW] Bind validation can be disabled and replaced with custom validators.
|
||||
- [NEW] More flexible HTML render
|
||||
- [NEW] Multipart and PostForm bindings
|
||||
- [NEW] Adds method to return all the registered routes
|
||||
- [NEW] Context.HandlerName() returns the main handler's name
|
||||
- [NEW] Adds Error.IsType() helper
|
||||
- [FIX] Binding multipart form
|
||||
- [FIX] Integration tests
|
||||
- [FIX] Crash when binding non struct object in Context.
|
||||
- [FIX] RunTLS() implementation
|
||||
- [FIX] Logger() unit tests
|
||||
- [FIX] Adds SetHTMLTemplate() warning
|
||||
- [FIX] Context.IsAborted()
|
||||
- [FIX] More unit tests
|
||||
- [FIX] JSON, XML, HTML renders accept custom content-types
|
||||
- [FIX] gin.AbortIndex is unexported
|
||||
- [FIX] Better approach to avoid directory listing in StaticFS()
|
||||
- [FIX] Context.ClientIP() always returns the IP with trimmed spaces.
|
||||
- [FIX] Better warning when running in debug mode.
|
||||
- [FIX] Google App Engine integration. debugPrint does not use os.Stdout
|
||||
- [FIX] Fixes integer overflow in error type
|
||||
- [FIX] Error implements the json.Marshaller interface
|
||||
- [FIX] MIT license in every file
|
||||
|
||||
|
||||
## Gin 1.0rc1 (May 22, 2015)
|
||||
|
||||
- [PERFORMANCE] Zero allocation router
|
||||
- [PERFORMANCE] Faster JSON, XML and text rendering
|
||||
- [PERFORMANCE] Custom hand optimized HttpRouter for Gin
|
||||
- [PERFORMANCE] Misc code optimizations. Inlining, tail call optimizations
|
||||
- [NEW] Built-in support for golang.org/x/net/context
|
||||
- [NEW] Any(path, handler). Create a route that matches any path
|
||||
- [NEW] Refactored rendering pipeline (faster and static typed)
|
||||
- [NEW] Refactored errors API
|
||||
- [NEW] IndentedJSON() prints pretty JSON
|
||||
- [NEW] Added gin.DefaultWriter
|
||||
- [NEW] UNIX socket support
|
||||
- [NEW] RouterGroup.BasePath is exposed
|
||||
- [NEW] JSON validation using go-validate-yourself (very powerful options)
|
||||
- [NEW] Completed suite of unit tests
|
||||
- [NEW] HTTP streaming with c.Stream()
|
||||
- [NEW] StaticFile() creates a router for serving just one file.
|
||||
- [NEW] StaticFS() has an option to disable directory listing.
|
||||
- [NEW] StaticFS() for serving static files through virtual filesystems
|
||||
- [NEW] Server-Sent Events native support
|
||||
- [NEW] WrapF() and WrapH() helpers for wrapping http.HandlerFunc and http.Handler
|
||||
- [NEW] Added LoggerWithWriter() middleware
|
||||
- [NEW] Added RecoveryWithWriter() middleware
|
||||
- [NEW] Added DefaultPostFormValue()
|
||||
- [NEW] Added DefaultFormValue()
|
||||
- [NEW] Added DefaultParamValue()
|
||||
- [FIX] BasicAuth() when using custom realm
|
||||
- [FIX] Bug when serving static files in nested routing group
|
||||
- [FIX] Redirect using built-in http.Redirect()
|
||||
- [FIX] Logger when printing the requested path
|
||||
- [FIX] Documentation typos
|
||||
- [FIX] Context.Engine renamed to Context.engine
|
||||
- [FIX] Better debugging messages
|
||||
- [FIX] ErrorLogger
|
||||
- [FIX] Debug HTTP render
|
||||
- [FIX] Refactored binding and render modules
|
||||
- [FIX] Refactored Context initialization
|
||||
- [FIX] Refactored BasicAuth()
|
||||
- [FIX] NoMethod/NoRoute handlers
|
||||
- [FIX] Hijacking http
|
||||
- [FIX] Better support for Google App Engine (using log instead of fmt)
|
||||
|
||||
|
||||
## Gin 0.6 (Mar 9, 2015)
|
||||
|
||||
- [NEW] Support multipart/form-data
|
||||
- [NEW] NoMethod handler
|
||||
- [NEW] Validate sub structures
|
||||
- [NEW] Support for HTTP Realm Auth
|
||||
- [FIX] Unsigned integers in binding
|
||||
- [FIX] Improve color logger
|
||||
|
||||
|
||||
## Gin 0.5 (Feb 7, 2015)
|
||||
|
||||
- [NEW] Content Negotiation
|
||||
- [FIX] Solved security bug that allow a client to spoof ip
|
||||
- [FIX] Fix unexported/ignored fields in binding
|
||||
|
||||
|
||||
## Gin 0.4 (Aug 21, 2014)
|
||||
|
||||
- [NEW] Development mode
|
||||
- [NEW] Unit tests
|
||||
- [NEW] Add Content.Redirect()
|
||||
- [FIX] Deferring WriteHeader()
|
||||
- [FIX] Improved documentation for model binding
|
||||
|
||||
|
||||
## Gin 0.3 (Jul 18, 2014)
|
||||
|
||||
- [PERFORMANCE] Normal log and error log are printed in the same call.
|
||||
- [PERFORMANCE] Improve performance of NoRouter()
|
||||
- [PERFORMANCE] Improve context's memory locality, reduce CPU cache faults.
|
||||
- [NEW] Flexible rendering API
|
||||
- [NEW] Add Context.File()
|
||||
- [NEW] Add shortcut RunTLS() for http.ListenAndServeTLS
|
||||
- [FIX] Rename NotFound404() to NoRoute()
|
||||
- [FIX] Errors in context are purged
|
||||
- [FIX] Adds HEAD method in Static file serving
|
||||
- [FIX] Refactors Static() file serving
|
||||
- [FIX] Using keyed initialization to fix app-engine integration
|
||||
- [FIX] Can't unmarshal JSON array, #63
|
||||
- [FIX] Renaming Context.Req to Context.Request
|
||||
- [FIX] Check application/x-www-form-urlencoded when parsing form
|
||||
|
||||
|
||||
## Gin 0.2b (Jul 08, 2014)
|
||||
- [PERFORMANCE] Using sync.Pool to allocatio/gc overhead
|
||||
- [NEW] Travis CI integration
|
||||
- [NEW] Completely new logger
|
||||
- [NEW] New API for serving static files. gin.Static()
|
||||
- [NEW] gin.H() can be serialized into XML
|
||||
- [NEW] Typed errors. Errors can be typed. Internet/external/custom.
|
||||
- [NEW] Support for Godeps
|
||||
- [NEW] Travis/Godocs badges in README
|
||||
- [NEW] New Bind() and BindWith() methods for parsing request body.
|
||||
- [NEW] Add Content.Copy()
|
||||
- [NEW] Add context.LastError()
|
||||
- [NEW] Add shortcut for OPTIONS HTTP method
|
||||
- [FIX] Tons of README fixes
|
||||
- [FIX] Header is written before body
|
||||
- [FIX] BasicAuth() and changes API a little bit
|
||||
- [FIX] Recovery() middleware only prints panics
|
||||
- [FIX] Context.Get() does not panic anymore. Use MustGet() instead.
|
||||
- [FIX] Multiple http.WriteHeader() in NotFound handlers
|
||||
- [FIX] Engine.Run() panics if http server can't be set up
|
||||
- [FIX] Crash when route path doesn't start with '/'
|
||||
- [FIX] Do not update header when status code is negative
|
||||
- [FIX] Setting response headers before calling WriteHeader in context.String()
|
||||
- [FIX] Add MIT license
|
||||
- [FIX] Changes behaviour of ErrorLogger() and Logger()
|
|
@ -1,46 +0,0 @@
|
|||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment include:
|
||||
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at teamgingonic@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
|
||||
|
||||
[homepage]: http://contributor-covenant.org
|
||||
[version]: http://contributor-covenant.org/version/1/4/
|
|
@ -1,13 +0,0 @@
|
|||
## Contributing
|
||||
|
||||
- With issues:
|
||||
- Use the search tool before opening a new issue.
|
||||
- Please provide source code and commit sha if you found a bug.
|
||||
- Review existing issues and provide feedback or react to them.
|
||||
|
||||
- With pull requests:
|
||||
- Open your pull request against `master`
|
||||
- Your pull request should have no more than two commits, if not you should squash them.
|
||||
- It should pass all tests in the available continuous integration systems such as GitHub Actions.
|
||||
- You should add/modify tests to cover your proposed code changes.
|
||||
- If your pull request contains a new feature, please document it on the README.
|
77
Makefile
77
Makefile
|
@ -1,77 +0,0 @@
|
|||
GO ?= go
|
||||
GOFMT ?= gofmt "-s"
|
||||
GO_VERSION=$(shell $(GO) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f2)
|
||||
PACKAGES ?= $(shell $(GO) list ./...)
|
||||
VETPACKAGES ?= $(shell $(GO) list ./... | grep -v /examples/)
|
||||
GOFILES := $(shell find . -name "*.go")
|
||||
TESTFOLDER := $(shell $(GO) list ./... | grep -E 'gin$$|binding$$|render$$' | grep -v examples)
|
||||
TESTTAGS ?= ""
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
echo "mode: count" > coverage.out
|
||||
for d in $(TESTFOLDER); do \
|
||||
$(GO) test $(TESTTAGS) -v -covermode=count -coverprofile=profile.out $$d > tmp.out; \
|
||||
cat tmp.out; \
|
||||
if grep -q "^--- FAIL" tmp.out; then \
|
||||
rm tmp.out; \
|
||||
exit 1; \
|
||||
elif grep -q "build failed" tmp.out; then \
|
||||
rm tmp.out; \
|
||||
exit 1; \
|
||||
elif grep -q "setup failed" tmp.out; then \
|
||||
rm tmp.out; \
|
||||
exit 1; \
|
||||
fi; \
|
||||
if [ -f profile.out ]; then \
|
||||
cat profile.out | grep -v "mode:" >> coverage.out; \
|
||||
rm profile.out; \
|
||||
fi; \
|
||||
done
|
||||
|
||||
.PHONY: fmt
|
||||
fmt:
|
||||
$(GOFMT) -w $(GOFILES)
|
||||
|
||||
.PHONY: fmt-check
|
||||
fmt-check:
|
||||
@diff=$$($(GOFMT) -d $(GOFILES)); \
|
||||
if [ -n "$$diff" ]; then \
|
||||
echo "Please run 'make fmt' and commit the result:"; \
|
||||
echo "$${diff}"; \
|
||||
exit 1; \
|
||||
fi;
|
||||
|
||||
vet:
|
||||
$(GO) vet $(VETPACKAGES)
|
||||
|
||||
.PHONY: lint
|
||||
lint:
|
||||
@hash golint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||
$(GO) get -u golang.org/x/lint/golint; \
|
||||
fi
|
||||
for PKG in $(PACKAGES); do golint -set_exit_status $$PKG || exit 1; done;
|
||||
|
||||
.PHONY: misspell-check
|
||||
misspell-check:
|
||||
@hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||
$(GO) get -u github.com/client9/misspell/cmd/misspell; \
|
||||
fi
|
||||
misspell -error $(GOFILES)
|
||||
|
||||
.PHONY: misspell
|
||||
misspell:
|
||||
@hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||
$(GO) get -u github.com/client9/misspell/cmd/misspell; \
|
||||
fi
|
||||
misspell -w $(GOFILES)
|
||||
|
||||
.PHONY: tools
|
||||
tools:
|
||||
@if [ $(GO_VERSION) -gt 15 ]; then \
|
||||
$(GO) install golang.org/x/lint/golint@latest; \
|
||||
$(GO) install github.com/client9/misspell/cmd/misspell@latest; \
|
||||
elif [ $(GO_VERSION) -lt 16 ]; then \
|
||||
$(GO) install golang.org/x/lint/golint; \
|
||||
$(GO) install github.com/client9/misspell/cmd/misspell; \
|
||||
fi
|
10
any.go
10
any.go
|
@ -1,10 +0,0 @@
|
|||
// Copyright 2022 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !go1.18
|
||||
// +build !go1.18
|
||||
|
||||
package gin
|
||||
|
||||
type any = interface{}
|
131
auth.go
131
auth.go
|
@ -1,91 +1,88 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.internal/re/gin/internal/bytesconv"
|
||||
"errors"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// AuthUserKey is the cookie name for user credential in basic auth.
|
||||
const AuthUserKey = "user"
|
||||
const (
|
||||
AuthUserKey = "user"
|
||||
)
|
||||
|
||||
// Accounts defines a key/value for user/pass list of authorized logins.
|
||||
type Accounts map[string]string
|
||||
type (
|
||||
BasicAuthPair struct {
|
||||
Code string
|
||||
User string
|
||||
}
|
||||
Accounts map[string]string
|
||||
Pairs []BasicAuthPair
|
||||
)
|
||||
|
||||
type authPair struct {
|
||||
value string
|
||||
user string
|
||||
func (a Pairs) Len() int { return len(a) }
|
||||
func (a Pairs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a Pairs) Less(i, j int) bool { return a[i].Code < a[j].Code }
|
||||
|
||||
func processCredentials(accounts Accounts) (Pairs, error) {
|
||||
if len(accounts) == 0 {
|
||||
return nil, errors.New("Empty list of authorized credentials.")
|
||||
}
|
||||
pairs := make(Pairs, 0, len(accounts))
|
||||
for user, password := range accounts {
|
||||
if len(user) == 0 || len(password) == 0 {
|
||||
return nil, errors.New("User or password is empty")
|
||||
}
|
||||
base := user + ":" + password
|
||||
code := "Basic " + base64.StdEncoding.EncodeToString([]byte(base))
|
||||
pairs = append(pairs, BasicAuthPair{code, user})
|
||||
}
|
||||
// We have to sort the credentials in order to use bsearch later.
|
||||
sort.Sort(pairs)
|
||||
return pairs, nil
|
||||
}
|
||||
|
||||
type authPairs []authPair
|
||||
|
||||
func (a authPairs) searchCredential(authValue string) (string, bool) {
|
||||
if authValue == "" {
|
||||
return "", false
|
||||
func secureCompare(given, actual string) bool {
|
||||
if subtle.ConstantTimeEq(int32(len(given)), int32(len(actual))) == 1 {
|
||||
return subtle.ConstantTimeCompare([]byte(given), []byte(actual)) == 1
|
||||
} else {
|
||||
/* Securely compare actual to itself to keep constant time, but always return false */
|
||||
return subtle.ConstantTimeCompare([]byte(actual), []byte(actual)) == 1 && false
|
||||
}
|
||||
for _, pair := range a {
|
||||
if subtle.ConstantTimeCompare(bytesconv.StringToBytes(pair.value), bytesconv.StringToBytes(authValue)) == 1 {
|
||||
return pair.user, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// BasicAuthForRealm returns a Basic HTTP Authorization middleware. It takes as arguments a map[string]string where
|
||||
// the key is the user name and the value is the password, as well as the name of the Realm.
|
||||
// If the realm is empty, "Authorization Required" will be used by default.
|
||||
// (see http://tools.ietf.org/html/rfc2617#section-1.2)
|
||||
func BasicAuthForRealm(accounts Accounts, realm string) HandlerFunc {
|
||||
if realm == "" {
|
||||
realm = "Authorization Required"
|
||||
func searchCredential(pairs Pairs, auth string) string {
|
||||
if len(auth) == 0 {
|
||||
return ""
|
||||
}
|
||||
realm = "Basic realm=" + strconv.Quote(realm)
|
||||
pairs := processAccounts(accounts)
|
||||
return func(c *Context) {
|
||||
// Search user in the slice of allowed credentials
|
||||
user, found := pairs.searchCredential(c.requestHeader("Authorization"))
|
||||
if !found {
|
||||
// Credentials doesn't match, we return 401 and abort handlers chain.
|
||||
c.Header("WWW-Authenticate", realm)
|
||||
c.AbortWithStatus(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// The user credentials was found, set user's id to key AuthUserKey in this context, the user's id can be read later using
|
||||
// c.MustGet(gin.AuthUserKey).
|
||||
c.Set(AuthUserKey, user)
|
||||
r := sort.Search(len(pairs), func(i int) bool { return pairs[i].Code >= auth })
|
||||
if r < len(pairs) && secureCompare(pairs[r].Code, auth) {
|
||||
return pairs[r].User
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// BasicAuth returns a Basic HTTP Authorization middleware. It takes as argument a map[string]string where
|
||||
// Implements a basic Basic HTTP Authorization. It takes as argument a map[string]string where
|
||||
// the key is the user name and the value is the password.
|
||||
func BasicAuth(accounts Accounts) HandlerFunc {
|
||||
return BasicAuthForRealm(accounts, "")
|
||||
}
|
||||
|
||||
func processAccounts(accounts Accounts) authPairs {
|
||||
length := len(accounts)
|
||||
assert1(length > 0, "Empty list of authorized credentials")
|
||||
pairs := make(authPairs, 0, length)
|
||||
for user, password := range accounts {
|
||||
assert1(user != "", "User can not be empty")
|
||||
value := authorizationHeader(user, password)
|
||||
pairs = append(pairs, authPair{
|
||||
value: value,
|
||||
user: user,
|
||||
})
|
||||
pairs, err := processCredentials(accounts)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return func(c *Context) {
|
||||
// Search user in the slice of allowed credentials
|
||||
user := searchCredential(pairs, c.Req.Header.Get("Authorization"))
|
||||
if len(user) == 0 {
|
||||
// Credentials doesn't match, we return 401 Unauthorized and abort request.
|
||||
c.Writer.Header().Set("WWW-Authenticate", "Basic realm=\"Authorization Required\"")
|
||||
c.Fail(401, errors.New("Unauthorized"))
|
||||
} else {
|
||||
// user is allowed, set UserId to key "user" in this context, the userId can be read later using
|
||||
// c.Get(gin.AuthUserKey)
|
||||
c.Set(AuthUserKey, user)
|
||||
}
|
||||
}
|
||||
return pairs
|
||||
}
|
||||
|
||||
func authorizationHeader(user, password string) string {
|
||||
base := user + ":" + password
|
||||
return "Basic " + base64.StdEncoding.EncodeToString(bytesconv.StringToBytes(base))
|
||||
}
|
||||
|
|
139
auth_test.go
139
auth_test.go
|
@ -1,139 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBasicAuth(t *testing.T) {
|
||||
pairs := processAccounts(Accounts{
|
||||
"admin": "password",
|
||||
"foo": "bar",
|
||||
"bar": "foo",
|
||||
})
|
||||
|
||||
assert.Len(t, pairs, 3)
|
||||
assert.Contains(t, pairs, authPair{
|
||||
user: "bar",
|
||||
value: "Basic YmFyOmZvbw==",
|
||||
})
|
||||
assert.Contains(t, pairs, authPair{
|
||||
user: "foo",
|
||||
value: "Basic Zm9vOmJhcg==",
|
||||
})
|
||||
assert.Contains(t, pairs, authPair{
|
||||
user: "admin",
|
||||
value: "Basic YWRtaW46cGFzc3dvcmQ=",
|
||||
})
|
||||
}
|
||||
|
||||
func TestBasicAuthFails(t *testing.T) {
|
||||
assert.Panics(t, func() { processAccounts(nil) })
|
||||
assert.Panics(t, func() {
|
||||
processAccounts(Accounts{
|
||||
"": "password",
|
||||
"foo": "bar",
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestBasicAuthSearchCredential(t *testing.T) {
|
||||
pairs := processAccounts(Accounts{
|
||||
"admin": "password",
|
||||
"foo": "bar",
|
||||
"bar": "foo",
|
||||
})
|
||||
|
||||
user, found := pairs.searchCredential(authorizationHeader("admin", "password"))
|
||||
assert.Equal(t, "admin", user)
|
||||
assert.True(t, found)
|
||||
|
||||
user, found = pairs.searchCredential(authorizationHeader("foo", "bar"))
|
||||
assert.Equal(t, "foo", user)
|
||||
assert.True(t, found)
|
||||
|
||||
user, found = pairs.searchCredential(authorizationHeader("bar", "foo"))
|
||||
assert.Equal(t, "bar", user)
|
||||
assert.True(t, found)
|
||||
|
||||
user, found = pairs.searchCredential(authorizationHeader("admins", "password"))
|
||||
assert.Empty(t, user)
|
||||
assert.False(t, found)
|
||||
|
||||
user, found = pairs.searchCredential(authorizationHeader("foo", "bar "))
|
||||
assert.Empty(t, user)
|
||||
assert.False(t, found)
|
||||
|
||||
user, found = pairs.searchCredential("")
|
||||
assert.Empty(t, user)
|
||||
assert.False(t, found)
|
||||
}
|
||||
|
||||
func TestBasicAuthAuthorizationHeader(t *testing.T) {
|
||||
assert.Equal(t, "Basic YWRtaW46cGFzc3dvcmQ=", authorizationHeader("admin", "password"))
|
||||
}
|
||||
|
||||
func TestBasicAuthSucceed(t *testing.T) {
|
||||
accounts := Accounts{"admin": "password"}
|
||||
router := New()
|
||||
router.Use(BasicAuth(accounts))
|
||||
router.GET("/login", func(c *Context) {
|
||||
c.String(http.StatusOK, c.MustGet(AuthUserKey).(string))
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/login", nil)
|
||||
req.Header.Set("Authorization", authorizationHeader("admin", "password"))
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Equal(t, "admin", w.Body.String())
|
||||
}
|
||||
|
||||
func TestBasicAuth401(t *testing.T) {
|
||||
called := false
|
||||
accounts := Accounts{"foo": "bar"}
|
||||
router := New()
|
||||
router.Use(BasicAuth(accounts))
|
||||
router.GET("/login", func(c *Context) {
|
||||
called = true
|
||||
c.String(http.StatusOK, c.MustGet(AuthUserKey).(string))
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/login", nil)
|
||||
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:password")))
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.False(t, called)
|
||||
assert.Equal(t, http.StatusUnauthorized, w.Code)
|
||||
assert.Equal(t, "Basic realm=\"Authorization Required\"", w.Header().Get("WWW-Authenticate"))
|
||||
}
|
||||
|
||||
func TestBasicAuth401WithCustomRealm(t *testing.T) {
|
||||
called := false
|
||||
accounts := Accounts{"foo": "bar"}
|
||||
router := New()
|
||||
router.Use(BasicAuthForRealm(accounts, "My Custom \"Realm\""))
|
||||
router.GET("/login", func(c *Context) {
|
||||
called = true
|
||||
c.String(http.StatusOK, c.MustGet(AuthUserKey).(string))
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/login", nil)
|
||||
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:password")))
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.False(t, called)
|
||||
assert.Equal(t, http.StatusUnauthorized, w.Code)
|
||||
assert.Equal(t, "Basic realm=\"My Custom \\\"Realm\\\"\"", w.Header().Get("WWW-Authenticate"))
|
||||
}
|
|
@ -1,160 +1,64 @@
|
|||
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func BenchmarkOneRoute(B *testing.B) {
|
||||
router := New()
|
||||
router.GET("/ping", func(c *Context) {})
|
||||
runRequest(B, router, "GET", "/ping")
|
||||
}
|
||||
func runHandler(B *testing.B, handler HandlerFunc) {
|
||||
req, err := http.NewRequest("GET", "http://localhost/foo", nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
c := &Context{
|
||||
Writer: &responseWriter{httptest.NewRecorder(), 0, false},
|
||||
Req: req,
|
||||
index: 0,
|
||||
}
|
||||
|
||||
func BenchmarkRecoveryMiddleware(B *testing.B) {
|
||||
router := New()
|
||||
router.Use(Recovery())
|
||||
router.GET("/", func(c *Context) {})
|
||||
runRequest(B, router, "GET", "/")
|
||||
}
|
||||
|
||||
func BenchmarkLoggerMiddleware(B *testing.B) {
|
||||
router := New()
|
||||
router.Use(LoggerWithWriter(newMockWriter()))
|
||||
router.GET("/", func(c *Context) {})
|
||||
runRequest(B, router, "GET", "/")
|
||||
}
|
||||
|
||||
func BenchmarkManyHandlers(B *testing.B) {
|
||||
router := New()
|
||||
router.Use(Recovery(), LoggerWithWriter(newMockWriter()))
|
||||
router.Use(func(c *Context) {})
|
||||
router.Use(func(c *Context) {})
|
||||
router.GET("/ping", func(c *Context) {})
|
||||
runRequest(B, router, "GET", "/ping")
|
||||
}
|
||||
|
||||
func Benchmark5Params(B *testing.B) {
|
||||
DefaultWriter = os.Stdout
|
||||
router := New()
|
||||
router.Use(func(c *Context) {})
|
||||
router.GET("/param/:param1/:params2/:param3/:param4/:param5", func(c *Context) {})
|
||||
runRequest(B, router, "GET", "/param/path/to/parameter/john/12345")
|
||||
}
|
||||
|
||||
func BenchmarkOneRouteJSON(B *testing.B) {
|
||||
router := New()
|
||||
data := struct {
|
||||
Status string `json:"status"`
|
||||
}{"ok"}
|
||||
router.GET("/json", func(c *Context) {
|
||||
c.JSON(http.StatusOK, data)
|
||||
})
|
||||
runRequest(B, router, "GET", "/json")
|
||||
}
|
||||
|
||||
func BenchmarkOneRouteHTML(B *testing.B) {
|
||||
router := New()
|
||||
t := template.Must(template.New("index").Parse(`
|
||||
<html><body><h1>{{.}}</h1></body></html>`))
|
||||
router.SetHTMLTemplate(t)
|
||||
|
||||
router.GET("/html", func(c *Context) {
|
||||
c.HTML(http.StatusOK, "index", "hola")
|
||||
})
|
||||
runRequest(B, router, "GET", "/html")
|
||||
}
|
||||
|
||||
func BenchmarkOneRouteSet(B *testing.B) {
|
||||
router := New()
|
||||
router.GET("/ping", func(c *Context) {
|
||||
c.Set("key", "value")
|
||||
})
|
||||
runRequest(B, router, "GET", "/ping")
|
||||
}
|
||||
|
||||
func BenchmarkOneRouteString(B *testing.B) {
|
||||
router := New()
|
||||
router.GET("/text", func(c *Context) {
|
||||
c.String(http.StatusOK, "this is a plain text")
|
||||
})
|
||||
runRequest(B, router, "GET", "/text")
|
||||
}
|
||||
|
||||
func BenchmarkManyRoutesFist(B *testing.B) {
|
||||
router := New()
|
||||
router.Any("/ping", func(c *Context) {})
|
||||
runRequest(B, router, "GET", "/ping")
|
||||
}
|
||||
|
||||
func BenchmarkManyRoutesLast(B *testing.B) {
|
||||
router := New()
|
||||
router.Any("/ping", func(c *Context) {})
|
||||
runRequest(B, router, "OPTIONS", "/ping")
|
||||
}
|
||||
|
||||
func Benchmark404(B *testing.B) {
|
||||
router := New()
|
||||
router.Any("/something", func(c *Context) {})
|
||||
router.NoRoute(func(c *Context) {})
|
||||
runRequest(B, router, "GET", "/ping")
|
||||
}
|
||||
|
||||
func Benchmark404Many(B *testing.B) {
|
||||
router := New()
|
||||
router.GET("/", func(c *Context) {})
|
||||
router.GET("/path/to/something", func(c *Context) {})
|
||||
router.GET("/post/:id", func(c *Context) {})
|
||||
router.GET("/view/:id", func(c *Context) {})
|
||||
router.GET("/favicon.ico", func(c *Context) {})
|
||||
router.GET("/robots.txt", func(c *Context) {})
|
||||
router.GET("/delete/:id", func(c *Context) {})
|
||||
router.GET("/user/:id/:mode", func(c *Context) {})
|
||||
|
||||
router.NoRoute(func(c *Context) {})
|
||||
runRequest(B, router, "GET", "/viewfake")
|
||||
}
|
||||
|
||||
type mockWriter struct {
|
||||
headers http.Header
|
||||
}
|
||||
|
||||
func newMockWriter() *mockWriter {
|
||||
return &mockWriter{
|
||||
http.Header{},
|
||||
B.ReportAllocs()
|
||||
B.ResetTimer()
|
||||
for i := 0; i < B.N; i++ {
|
||||
c.index = 0
|
||||
handler(c)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *mockWriter) Header() (h http.Header) {
|
||||
return m.headers
|
||||
}
|
||||
|
||||
func (m *mockWriter) Write(p []byte) (n int, err error) {
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func (m *mockWriter) WriteString(s string) (n int, err error) {
|
||||
return len(s), nil
|
||||
}
|
||||
|
||||
func (m *mockWriter) WriteHeader(int) {}
|
||||
|
||||
func runRequest(B *testing.B, r *Engine, method, path string) {
|
||||
func runRequest(B *testing.B, r *Engine, path string) {
|
||||
// create fake request
|
||||
req, err := http.NewRequest(method, path, nil)
|
||||
url := "http://localhost" + path
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
w := newMockWriter()
|
||||
// create fake writes
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
B.ReportAllocs()
|
||||
B.ResetTimer()
|
||||
for i := 0; i < B.N; i++ {
|
||||
r.ServeHTTP(w, req)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMiddlewareLogger(B *testing.B) {
|
||||
runHandler(B, Logger())
|
||||
}
|
||||
|
||||
func BenchmarkDefaultOnlyPing(B *testing.B) {
|
||||
r := New()
|
||||
r.GET("/ping", func(c *Context) {
|
||||
c.String(200, "pong")
|
||||
})
|
||||
runRequest(B, r, "/ping")
|
||||
}
|
||||
|
||||
func BenchmarkDefaultPing(B *testing.B) {
|
||||
r := Default()
|
||||
r.GET("/ping", func(c *Context) {
|
||||
c.String(200, "pong")
|
||||
})
|
||||
runRequest(B, r, "/ping")
|
||||
}
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
// Copyright 2022 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !go1.18
|
||||
// +build !go1.18
|
||||
|
||||
package binding
|
||||
|
||||
type any = interface{}
|
|
@ -1,122 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !nomsgpack
|
||||
// +build !nomsgpack
|
||||
|
||||
package binding
|
||||
|
||||
import "net/http"
|
||||
|
||||
// Content-Type MIME of the most common data formats.
|
||||
const (
|
||||
MIMEJSON = "application/json"
|
||||
MIMEHTML = "text/html"
|
||||
MIMEXML = "application/xml"
|
||||
MIMEXML2 = "text/xml"
|
||||
MIMEPlain = "text/plain"
|
||||
MIMEPOSTForm = "application/x-www-form-urlencoded"
|
||||
MIMEMultipartPOSTForm = "multipart/form-data"
|
||||
MIMEPROTOBUF = "application/x-protobuf"
|
||||
MIMEMSGPACK = "application/x-msgpack"
|
||||
MIMEMSGPACK2 = "application/msgpack"
|
||||
MIMEYAML = "application/x-yaml"
|
||||
MIMETOML = "application/toml"
|
||||
)
|
||||
|
||||
// Binding describes the interface which needs to be implemented for binding the
|
||||
// data present in the request such as JSON request body, query parameters or
|
||||
// the form POST.
|
||||
type Binding interface {
|
||||
Name() string
|
||||
Bind(*http.Request, any) error
|
||||
}
|
||||
|
||||
// BindingBody adds BindBody method to Binding. BindBody is similar with Bind,
|
||||
// but it reads the body from supplied bytes instead of req.Body.
|
||||
type BindingBody interface {
|
||||
Binding
|
||||
BindBody([]byte, any) error
|
||||
}
|
||||
|
||||
// BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
|
||||
// but it reads the Params.
|
||||
type BindingUri interface {
|
||||
Name() string
|
||||
BindUri(map[string][]string, any) error
|
||||
}
|
||||
|
||||
// StructValidator is the minimal interface which needs to be implemented in
|
||||
// order for it to be used as the validator engine for ensuring the correctness
|
||||
// of the request. Gin provides a default implementation for this using
|
||||
// https://github.com/go-playground/validator/tree/v10.6.1.
|
||||
type StructValidator interface {
|
||||
// ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
|
||||
// If the received type is a slice|array, the validation should be performed travel on every element.
|
||||
// If the received type is not a struct or slice|array, any validation should be skipped and nil must be returned.
|
||||
// If the received type is a struct or pointer to a struct, the validation should be performed.
|
||||
// If the struct is not valid or the validation itself fails, a descriptive error should be returned.
|
||||
// Otherwise nil must be returned.
|
||||
ValidateStruct(any) error
|
||||
|
||||
// Engine returns the underlying validator engine which powers the
|
||||
// StructValidator implementation.
|
||||
Engine() any
|
||||
}
|
||||
|
||||
// Validator is the default validator which implements the StructValidator
|
||||
// interface. It uses https://github.com/go-playground/validator/tree/v10.6.1
|
||||
// under the hood.
|
||||
var Validator StructValidator = &defaultValidator{}
|
||||
|
||||
// These implement the Binding interface and can be used to bind the data
|
||||
// present in the request to struct instances.
|
||||
var (
|
||||
JSON = jsonBinding{}
|
||||
XML = xmlBinding{}
|
||||
Form = formBinding{}
|
||||
Query = queryBinding{}
|
||||
FormPost = formPostBinding{}
|
||||
FormMultipart = formMultipartBinding{}
|
||||
ProtoBuf = protobufBinding{}
|
||||
MsgPack = msgpackBinding{}
|
||||
YAML = yamlBinding{}
|
||||
Uri = uriBinding{}
|
||||
Header = headerBinding{}
|
||||
TOML = tomlBinding{}
|
||||
)
|
||||
|
||||
// Default returns the appropriate Binding instance based on the HTTP method
|
||||
// and the content type.
|
||||
func Default(method, contentType string) Binding {
|
||||
if method == http.MethodGet {
|
||||
return Form
|
||||
}
|
||||
|
||||
switch contentType {
|
||||
case MIMEJSON:
|
||||
return JSON
|
||||
case MIMEXML, MIMEXML2:
|
||||
return XML
|
||||
case MIMEPROTOBUF:
|
||||
return ProtoBuf
|
||||
case MIMEMSGPACK, MIMEMSGPACK2:
|
||||
return MsgPack
|
||||
case MIMEYAML:
|
||||
return YAML
|
||||
case MIMETOML:
|
||||
return TOML
|
||||
case MIMEMultipartPOSTForm:
|
||||
return FormMultipart
|
||||
default: // case MIMEPOSTForm:
|
||||
return Form
|
||||
}
|
||||
}
|
||||
|
||||
func validate(obj any) error {
|
||||
if Validator == nil {
|
||||
return nil
|
||||
}
|
||||
return Validator.ValidateStruct(obj)
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
// Copyright 2020 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !nomsgpack
|
||||
// +build !nomsgpack
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/ugorji/go/codec"
|
||||
)
|
||||
|
||||
func TestBindingMsgPack(t *testing.T) {
|
||||
test := FooStruct{
|
||||
Foo: "bar",
|
||||
}
|
||||
|
||||
h := new(codec.MsgpackHandle)
|
||||
assert.NotNil(t, h)
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
assert.NotNil(t, buf)
|
||||
err := codec.NewEncoder(buf, h).Encode(test)
|
||||
assert.NoError(t, err)
|
||||
|
||||
data := buf.Bytes()
|
||||
|
||||
testMsgPackBodyBinding(t,
|
||||
MsgPack, "msgpack",
|
||||
"/", "/",
|
||||
string(data), string(data[1:]))
|
||||
}
|
||||
|
||||
func testMsgPackBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
|
||||
assert.Equal(t, name, b.Name())
|
||||
|
||||
obj := FooStruct{}
|
||||
req := requestWithBody("POST", path, body)
|
||||
req.Header.Add("Content-Type", MIMEMSGPACK)
|
||||
err := b.Bind(req, &obj)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "bar", obj.Foo)
|
||||
|
||||
obj = FooStruct{}
|
||||
req = requestWithBody("POST", badPath, badBody)
|
||||
req.Header.Add("Content-Type", MIMEMSGPACK)
|
||||
err = MsgPack.Bind(req, &obj)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestBindingDefaultMsgPack(t *testing.T) {
|
||||
assert.Equal(t, MsgPack, Default("POST", MIMEMSGPACK))
|
||||
assert.Equal(t, MsgPack, Default("PUT", MIMEMSGPACK2))
|
||||
}
|
|
@ -1,116 +0,0 @@
|
|||
// Copyright 2020 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build nomsgpack
|
||||
// +build nomsgpack
|
||||
|
||||
package binding
|
||||
|
||||
import "net/http"
|
||||
|
||||
// Content-Type MIME of the most common data formats.
|
||||
const (
|
||||
MIMEJSON = "application/json"
|
||||
MIMEHTML = "text/html"
|
||||
MIMEXML = "application/xml"
|
||||
MIMEXML2 = "text/xml"
|
||||
MIMEPlain = "text/plain"
|
||||
MIMEPOSTForm = "application/x-www-form-urlencoded"
|
||||
MIMEMultipartPOSTForm = "multipart/form-data"
|
||||
MIMEPROTOBUF = "application/x-protobuf"
|
||||
MIMEYAML = "application/x-yaml"
|
||||
MIMETOML = "application/toml"
|
||||
)
|
||||
|
||||
// Binding describes the interface which needs to be implemented for binding the
|
||||
// data present in the request such as JSON request body, query parameters or
|
||||
// the form POST.
|
||||
type Binding interface {
|
||||
Name() string
|
||||
Bind(*http.Request, any) error
|
||||
}
|
||||
|
||||
// BindingBody adds BindBody method to Binding. BindBody is similar with Bind,
|
||||
// but it reads the body from supplied bytes instead of req.Body.
|
||||
type BindingBody interface {
|
||||
Binding
|
||||
BindBody([]byte, any) error
|
||||
}
|
||||
|
||||
// BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
|
||||
// but it reads the Params.
|
||||
type BindingUri interface {
|
||||
Name() string
|
||||
BindUri(map[string][]string, any) error
|
||||
}
|
||||
|
||||
// StructValidator is the minimal interface which needs to be implemented in
|
||||
// order for it to be used as the validator engine for ensuring the correctness
|
||||
// of the request. Gin provides a default implementation for this using
|
||||
// https://github.com/go-playground/validator/tree/v10.6.1.
|
||||
type StructValidator interface {
|
||||
// ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
|
||||
// If the received type is not a struct, any validation should be skipped and nil must be returned.
|
||||
// If the received type is a struct or pointer to a struct, the validation should be performed.
|
||||
// If the struct is not valid or the validation itself fails, a descriptive error should be returned.
|
||||
// Otherwise nil must be returned.
|
||||
ValidateStruct(any) error
|
||||
|
||||
// Engine returns the underlying validator engine which powers the
|
||||
// StructValidator implementation.
|
||||
Engine() any
|
||||
}
|
||||
|
||||
// Validator is the default validator which implements the StructValidator
|
||||
// interface. It uses https://github.com/go-playground/validator/tree/v10.6.1
|
||||
// under the hood.
|
||||
var Validator StructValidator = &defaultValidator{}
|
||||
|
||||
// These implement the Binding interface and can be used to bind the data
|
||||
// present in the request to struct instances.
|
||||
var (
|
||||
JSON = jsonBinding{}
|
||||
XML = xmlBinding{}
|
||||
Form = formBinding{}
|
||||
Query = queryBinding{}
|
||||
FormPost = formPostBinding{}
|
||||
FormMultipart = formMultipartBinding{}
|
||||
ProtoBuf = protobufBinding{}
|
||||
YAML = yamlBinding{}
|
||||
Uri = uriBinding{}
|
||||
Header = headerBinding{}
|
||||
TOML = tomlBinding{}
|
||||
)
|
||||
|
||||
// Default returns the appropriate Binding instance based on the HTTP method
|
||||
// and the content type.
|
||||
func Default(method, contentType string) Binding {
|
||||
if method == "GET" {
|
||||
return Form
|
||||
}
|
||||
|
||||
switch contentType {
|
||||
case MIMEJSON:
|
||||
return JSON
|
||||
case MIMEXML, MIMEXML2:
|
||||
return XML
|
||||
case MIMEPROTOBUF:
|
||||
return ProtoBuf
|
||||
case MIMEYAML:
|
||||
return YAML
|
||||
case MIMEMultipartPOSTForm:
|
||||
return FormMultipart
|
||||
case MIMETOML:
|
||||
return TOML
|
||||
default: // case MIMEPOSTForm:
|
||||
return Form
|
||||
}
|
||||
}
|
||||
|
||||
func validate(obj any) error {
|
||||
if Validator == nil {
|
||||
return nil
|
||||
}
|
||||
return Validator.ValidateStruct(obj)
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -1,97 +0,0 @@
|
|||
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
type defaultValidator struct {
|
||||
once sync.Once
|
||||
validate *validator.Validate
|
||||
}
|
||||
|
||||
type SliceValidationError []error
|
||||
|
||||
// Error concatenates all error elements in SliceValidationError into a single string separated by \n.
|
||||
func (err SliceValidationError) Error() string {
|
||||
n := len(err)
|
||||
switch n {
|
||||
case 0:
|
||||
return ""
|
||||
default:
|
||||
var b strings.Builder
|
||||
if err[0] != nil {
|
||||
fmt.Fprintf(&b, "[%d]: %s", 0, err[0].Error())
|
||||
}
|
||||
if n > 1 {
|
||||
for i := 1; i < n; i++ {
|
||||
if err[i] != nil {
|
||||
b.WriteString("\n")
|
||||
fmt.Fprintf(&b, "[%d]: %s", i, err[i].Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
}
|
||||
|
||||
var _ StructValidator = (*defaultValidator)(nil)
|
||||
|
||||
// ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.
|
||||
func (v *defaultValidator) ValidateStruct(obj any) error {
|
||||
if obj == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
value := reflect.ValueOf(obj)
|
||||
switch value.Kind() {
|
||||
case reflect.Ptr:
|
||||
return v.ValidateStruct(value.Elem().Interface())
|
||||
case reflect.Struct:
|
||||
return v.validateStruct(obj)
|
||||
case reflect.Slice, reflect.Array:
|
||||
count := value.Len()
|
||||
validateRet := make(SliceValidationError, 0)
|
||||
for i := 0; i < count; i++ {
|
||||
if err := v.ValidateStruct(value.Index(i).Interface()); err != nil {
|
||||
validateRet = append(validateRet, err)
|
||||
}
|
||||
}
|
||||
if len(validateRet) == 0 {
|
||||
return nil
|
||||
}
|
||||
return validateRet
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// validateStruct receives struct type
|
||||
func (v *defaultValidator) validateStruct(obj any) error {
|
||||
v.lazyinit()
|
||||
return v.validate.Struct(obj)
|
||||
}
|
||||
|
||||
// Engine returns the underlying validator engine which powers the default
|
||||
// Validator instance. This is useful if you want to register custom validations
|
||||
// or struct level validations. See validator GoDoc for more info -
|
||||
// https://pkg.go.dev/github.com/go-playground/validator/v10
|
||||
func (v *defaultValidator) Engine() any {
|
||||
v.lazyinit()
|
||||
return v.validate
|
||||
}
|
||||
|
||||
func (v *defaultValidator) lazyinit() {
|
||||
v.once.Do(func() {
|
||||
v.validate = validator.New()
|
||||
v.validate.SetTagName("binding")
|
||||
})
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
// Copyright 2022 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func BenchmarkSliceValidationError(b *testing.B) {
|
||||
const size int = 100
|
||||
for i := 0; i < b.N; i++ {
|
||||
e := make(SliceValidationError, size)
|
||||
for j := 0; j < size; j++ {
|
||||
e[j] = errors.New(strconv.Itoa(j))
|
||||
}
|
||||
if len(e.Error()) == 0 {
|
||||
b.Errorf("error")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,88 +0,0 @@
|
|||
// Copyright 2020 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSliceValidationError(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err SliceValidationError
|
||||
want string
|
||||
}{
|
||||
{"has nil elements", SliceValidationError{errors.New("test error"), nil}, "[0]: test error"},
|
||||
{"has zero elements", SliceValidationError{}, ""},
|
||||
{"has one element", SliceValidationError{errors.New("test one error")}, "[0]: test one error"},
|
||||
{"has two elements",
|
||||
SliceValidationError{
|
||||
errors.New("first error"),
|
||||
errors.New("second error"),
|
||||
},
|
||||
"[0]: first error\n[1]: second error",
|
||||
},
|
||||
{"has many elements",
|
||||
SliceValidationError{
|
||||
errors.New("first error"),
|
||||
errors.New("second error"),
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
errors.New("last error"),
|
||||
},
|
||||
"[0]: first error\n[1]: second error\n[5]: last error",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.err.Error(); got != tt.want {
|
||||
t.Errorf("SliceValidationError.Error() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultValidator(t *testing.T) {
|
||||
type exampleStruct struct {
|
||||
A string `binding:"max=8"`
|
||||
B int `binding:"gt=0"`
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
v *defaultValidator
|
||||
obj any
|
||||
wantErr bool
|
||||
}{
|
||||
{"validate nil obj", &defaultValidator{}, nil, false},
|
||||
{"validate int obj", &defaultValidator{}, 3, false},
|
||||
{"validate struct failed-1", &defaultValidator{}, exampleStruct{A: "123456789", B: 1}, true},
|
||||
{"validate struct failed-2", &defaultValidator{}, exampleStruct{A: "12345678", B: 0}, true},
|
||||
{"validate struct passed", &defaultValidator{}, exampleStruct{A: "12345678", B: 1}, false},
|
||||
{"validate *struct failed-1", &defaultValidator{}, &exampleStruct{A: "123456789", B: 1}, true},
|
||||
{"validate *struct failed-2", &defaultValidator{}, &exampleStruct{A: "12345678", B: 0}, true},
|
||||
{"validate *struct passed", &defaultValidator{}, &exampleStruct{A: "12345678", B: 1}, false},
|
||||
{"validate []struct failed-1", &defaultValidator{}, []exampleStruct{{A: "123456789", B: 1}}, true},
|
||||
{"validate []struct failed-2", &defaultValidator{}, []exampleStruct{{A: "12345678", B: 0}}, true},
|
||||
{"validate []struct passed", &defaultValidator{}, []exampleStruct{{A: "12345678", B: 1}}, false},
|
||||
{"validate []*struct failed-1", &defaultValidator{}, []*exampleStruct{{A: "123456789", B: 1}}, true},
|
||||
{"validate []*struct failed-2", &defaultValidator{}, []*exampleStruct{{A: "12345678", B: 0}}, true},
|
||||
{"validate []*struct passed", &defaultValidator{}, []*exampleStruct{{A: "12345678", B: 1}}, false},
|
||||
{"validate *[]struct failed-1", &defaultValidator{}, &[]exampleStruct{{A: "123456789", B: 1}}, true},
|
||||
{"validate *[]struct failed-2", &defaultValidator{}, &[]exampleStruct{{A: "12345678", B: 0}}, true},
|
||||
{"validate *[]struct passed", &defaultValidator{}, &[]exampleStruct{{A: "12345678", B: 1}}, false},
|
||||
{"validate *[]*struct failed-1", &defaultValidator{}, &[]*exampleStruct{{A: "123456789", B: 1}}, true},
|
||||
{"validate *[]*struct failed-2", &defaultValidator{}, &[]*exampleStruct{{A: "12345678", B: 0}}, true},
|
||||
{"validate *[]*struct passed", &defaultValidator{}, &[]*exampleStruct{{A: "12345678", B: 1}}, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := tt.v.ValidateStruct(tt.obj); (err != nil) != tt.wantErr {
|
||||
t.Errorf("defaultValidator.Validate() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const defaultMemory = 32 << 20
|
||||
|
||||
type formBinding struct{}
|
||||
type formPostBinding struct{}
|
||||
type formMultipartBinding struct{}
|
||||
|
||||
func (formBinding) Name() string {
|
||||
return "form"
|
||||
}
|
||||
|
||||
func (formBinding) Bind(req *http.Request, obj any) error {
|
||||
if err := req.ParseForm(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := req.ParseMultipartForm(defaultMemory); err != nil && !errors.Is(err, http.ErrNotMultipart) {
|
||||
return err
|
||||
}
|
||||
if err := mapForm(obj, req.Form); err != nil {
|
||||
return err
|
||||
}
|
||||
return validate(obj)
|
||||
}
|
||||
|
||||
func (formPostBinding) Name() string {
|
||||
return "form-urlencoded"
|
||||
}
|
||||
|
||||
func (formPostBinding) Bind(req *http.Request, obj any) error {
|
||||
if err := req.ParseForm(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := mapForm(obj, req.PostForm); err != nil {
|
||||
return err
|
||||
}
|
||||
return validate(obj)
|
||||
}
|
||||
|
||||
func (formMultipartBinding) Name() string {
|
||||
return "multipart/form-data"
|
||||
}
|
||||
|
||||
func (formMultipartBinding) Bind(req *http.Request, obj any) error {
|
||||
if err := req.ParseMultipartForm(defaultMemory); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := mappingByPtr(obj, (*multipartRequest)(req), "form"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return validate(obj)
|
||||
}
|
|
@ -1,403 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.internal/re/gin/internal/bytesconv"
|
||||
"git.internal/re/gin/internal/json"
|
||||
)
|
||||
|
||||
var (
|
||||
errUnknownType = errors.New("unknown type")
|
||||
|
||||
// ErrConvertMapStringSlice can not convert to map[string][]string
|
||||
ErrConvertMapStringSlice = errors.New("can not convert to map slices of strings")
|
||||
|
||||
// ErrConvertToMapString can not convert to map[string]string
|
||||
ErrConvertToMapString = errors.New("can not convert to map of strings")
|
||||
)
|
||||
|
||||
func mapURI(ptr any, m map[string][]string) error {
|
||||
return mapFormByTag(ptr, m, "uri")
|
||||
}
|
||||
|
||||
func mapForm(ptr any, form map[string][]string) error {
|
||||
return mapFormByTag(ptr, form, "form")
|
||||
}
|
||||
|
||||
func MapFormWithTag(ptr any, form map[string][]string, tag string) error {
|
||||
return mapFormByTag(ptr, form, tag)
|
||||
}
|
||||
|
||||
var emptyField = reflect.StructField{}
|
||||
|
||||
func mapFormByTag(ptr any, form map[string][]string, tag string) error {
|
||||
// Check if ptr is a map
|
||||
ptrVal := reflect.ValueOf(ptr)
|
||||
var pointed any
|
||||
if ptrVal.Kind() == reflect.Ptr {
|
||||
ptrVal = ptrVal.Elem()
|
||||
pointed = ptrVal.Interface()
|
||||
}
|
||||
if ptrVal.Kind() == reflect.Map &&
|
||||
ptrVal.Type().Key().Kind() == reflect.String {
|
||||
if pointed != nil {
|
||||
ptr = pointed
|
||||
}
|
||||
return setFormMap(ptr, form)
|
||||
}
|
||||
|
||||
return mappingByPtr(ptr, formSource(form), tag)
|
||||
}
|
||||
|
||||
// setter tries to set value on a walking by fields of a struct
|
||||
type setter interface {
|
||||
TrySet(value reflect.Value, field reflect.StructField, key string, opt setOptions) (isSet bool, err error)
|
||||
}
|
||||
|
||||
type formSource map[string][]string
|
||||
|
||||
var _ setter = formSource(nil)
|
||||
|
||||
// TrySet tries to set a value by request's form source (like map[string][]string)
|
||||
func (form formSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt setOptions) (isSet bool, err error) {
|
||||
return setByForm(value, field, form, tagValue, opt)
|
||||
}
|
||||
|
||||
func mappingByPtr(ptr any, setter setter, tag string) error {
|
||||
_, err := mapping(reflect.ValueOf(ptr), emptyField, setter, tag)
|
||||
return err
|
||||
}
|
||||
|
||||
func mapping(value reflect.Value, field reflect.StructField, setter setter, tag string) (bool, error) {
|
||||
if field.Tag.Get(tag) == "-" { // just ignoring this field
|
||||
return false, nil
|
||||
}
|
||||
|
||||
vKind := value.Kind()
|
||||
|
||||
if vKind == reflect.Ptr {
|
||||
var isNew bool
|
||||
vPtr := value
|
||||
if value.IsNil() {
|
||||
isNew = true
|
||||
vPtr = reflect.New(value.Type().Elem())
|
||||
}
|
||||
isSet, err := mapping(vPtr.Elem(), field, setter, tag)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if isNew && isSet {
|
||||
value.Set(vPtr)
|
||||
}
|
||||
return isSet, nil
|
||||
}
|
||||
|
||||
if vKind != reflect.Struct || !field.Anonymous {
|
||||
ok, err := tryToSetValue(value, field, setter, tag)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if ok {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
if vKind == reflect.Struct {
|
||||
tValue := value.Type()
|
||||
|
||||
var isSet bool
|
||||
for i := 0; i < value.NumField(); i++ {
|
||||
sf := tValue.Field(i)
|
||||
if sf.PkgPath != "" && !sf.Anonymous { // unexported
|
||||
continue
|
||||
}
|
||||
ok, err := mapping(value.Field(i), sf, setter, tag)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
isSet = isSet || ok
|
||||
}
|
||||
return isSet, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
type setOptions struct {
|
||||
isDefaultExists bool
|
||||
defaultValue string
|
||||
}
|
||||
|
||||
func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter, tag string) (bool, error) {
|
||||
var tagValue string
|
||||
var setOpt setOptions
|
||||
|
||||
tagValue = field.Tag.Get(tag)
|
||||
tagValue, opts := head(tagValue, ",")
|
||||
|
||||
if tagValue == "" { // default value is FieldName
|
||||
tagValue = field.Name
|
||||
}
|
||||
if tagValue == "" { // when field is "emptyField" variable
|
||||
return false, nil
|
||||
}
|
||||
|
||||
var opt string
|
||||
for len(opts) > 0 {
|
||||
opt, opts = head(opts, ",")
|
||||
|
||||
if k, v := head(opt, "="); k == "default" {
|
||||
setOpt.isDefaultExists = true
|
||||
setOpt.defaultValue = v
|
||||
}
|
||||
}
|
||||
|
||||
return setter.TrySet(value, field, tagValue, setOpt)
|
||||
}
|
||||
|
||||
func setByForm(value reflect.Value, field reflect.StructField, form map[string][]string, tagValue string, opt setOptions) (isSet bool, err error) {
|
||||
vs, ok := form[tagValue]
|
||||
if !ok && !opt.isDefaultExists {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
switch value.Kind() {
|
||||
case reflect.Slice:
|
||||
if !ok {
|
||||
vs = []string{opt.defaultValue}
|
||||
}
|
||||
return true, setSlice(vs, value, field)
|
||||
case reflect.Array:
|
||||
if !ok {
|
||||
vs = []string{opt.defaultValue}
|
||||
}
|
||||
if len(vs) != value.Len() {
|
||||
return false, fmt.Errorf("%q is not valid value for %s", vs, value.Type().String())
|
||||
}
|
||||
return true, setArray(vs, value, field)
|
||||
default:
|
||||
var val string
|
||||
if !ok {
|
||||
val = opt.defaultValue
|
||||
}
|
||||
|
||||
if len(vs) > 0 {
|
||||
val = vs[0]
|
||||
}
|
||||
return true, setWithProperType(val, value, field)
|
||||
}
|
||||
}
|
||||
|
||||
func setWithProperType(val string, value reflect.Value, field reflect.StructField) error {
|
||||
switch value.Kind() {
|
||||
case reflect.Int:
|
||||
return setIntField(val, 0, value)
|
||||
case reflect.Int8:
|
||||
return setIntField(val, 8, value)
|
||||
case reflect.Int16:
|
||||
return setIntField(val, 16, value)
|
||||
case reflect.Int32:
|
||||
return setIntField(val, 32, value)
|
||||
case reflect.Int64:
|
||||
switch value.Interface().(type) {
|
||||
case time.Duration:
|
||||
return setTimeDuration(val, value)
|
||||
}
|
||||
return setIntField(val, 64, value)
|
||||
case reflect.Uint:
|
||||
return setUintField(val, 0, value)
|
||||
case reflect.Uint8:
|
||||
return setUintField(val, 8, value)
|
||||
case reflect.Uint16:
|
||||
return setUintField(val, 16, value)
|
||||
case reflect.Uint32:
|
||||
return setUintField(val, 32, value)
|
||||
case reflect.Uint64:
|
||||
return setUintField(val, 64, value)
|
||||
case reflect.Bool:
|
||||
return setBoolField(val, value)
|
||||
case reflect.Float32:
|
||||
return setFloatField(val, 32, value)
|
||||
case reflect.Float64:
|
||||
return setFloatField(val, 64, value)
|
||||
case reflect.String:
|
||||
value.SetString(val)
|
||||
case reflect.Struct:
|
||||
switch value.Interface().(type) {
|
||||
case time.Time:
|
||||
return setTimeField(val, field, value)
|
||||
}
|
||||
return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())
|
||||
case reflect.Map:
|
||||
return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())
|
||||
default:
|
||||
return errUnknownType
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setIntField(val string, bitSize int, field reflect.Value) error {
|
||||
if val == "" {
|
||||
val = "0"
|
||||
}
|
||||
intVal, err := strconv.ParseInt(val, 10, bitSize)
|
||||
if err == nil {
|
||||
field.SetInt(intVal)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func setUintField(val string, bitSize int, field reflect.Value) error {
|
||||
if val == "" {
|
||||
val = "0"
|
||||
}
|
||||
uintVal, err := strconv.ParseUint(val, 10, bitSize)
|
||||
if err == nil {
|
||||
field.SetUint(uintVal)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func setBoolField(val string, field reflect.Value) error {
|
||||
if val == "" {
|
||||
val = "false"
|
||||
}
|
||||
boolVal, err := strconv.ParseBool(val)
|
||||
if err == nil {
|
||||
field.SetBool(boolVal)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func setFloatField(val string, bitSize int, field reflect.Value) error {
|
||||
if val == "" {
|
||||
val = "0.0"
|
||||
}
|
||||
floatVal, err := strconv.ParseFloat(val, bitSize)
|
||||
if err == nil {
|
||||
field.SetFloat(floatVal)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func setTimeField(val string, structField reflect.StructField, value reflect.Value) error {
|
||||
timeFormat := structField.Tag.Get("time_format")
|
||||
if timeFormat == "" {
|
||||
timeFormat = time.RFC3339
|
||||
}
|
||||
|
||||
switch tf := strings.ToLower(timeFormat); tf {
|
||||
case "unix", "unixnano":
|
||||
tv, err := strconv.ParseInt(val, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d := time.Duration(1)
|
||||
if tf == "unixnano" {
|
||||
d = time.Second
|
||||
}
|
||||
|
||||
t := time.Unix(tv/int64(d), tv%int64(d))
|
||||
value.Set(reflect.ValueOf(t))
|
||||
return nil
|
||||
}
|
||||
|
||||
if val == "" {
|
||||
value.Set(reflect.ValueOf(time.Time{}))
|
||||
return nil
|
||||
}
|
||||
|
||||
l := time.Local
|
||||
if isUTC, _ := strconv.ParseBool(structField.Tag.Get("time_utc")); isUTC {
|
||||
l = time.UTC
|
||||
}
|
||||
|
||||
if locTag := structField.Tag.Get("time_location"); locTag != "" {
|
||||
loc, err := time.LoadLocation(locTag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
l = loc
|
||||
}
|
||||
|
||||
t, err := time.ParseInLocation(timeFormat, val, l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
value.Set(reflect.ValueOf(t))
|
||||
return nil
|
||||
}
|
||||
|
||||
func setArray(vals []string, value reflect.Value, field reflect.StructField) error {
|
||||
for i, s := range vals {
|
||||
err := setWithProperType(s, value.Index(i), field)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setSlice(vals []string, value reflect.Value, field reflect.StructField) error {
|
||||
slice := reflect.MakeSlice(value.Type(), len(vals), len(vals))
|
||||
err := setArray(vals, slice, field)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
value.Set(slice)
|
||||
return nil
|
||||
}
|
||||
|
||||
func setTimeDuration(val string, value reflect.Value) error {
|
||||
d, err := time.ParseDuration(val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
value.Set(reflect.ValueOf(d))
|
||||
return nil
|
||||
}
|
||||
|
||||
func head(str, sep string) (head string, tail string) {
|
||||
idx := strings.Index(str, sep)
|
||||
if idx < 0 {
|
||||
return str, ""
|
||||
}
|
||||
return str[:idx], str[idx+len(sep):]
|
||||
}
|
||||
|
||||
func setFormMap(ptr any, form map[string][]string) error {
|
||||
el := reflect.TypeOf(ptr).Elem()
|
||||
|
||||
if el.Kind() == reflect.Slice {
|
||||
ptrMap, ok := ptr.(map[string][]string)
|
||||
if !ok {
|
||||
return ErrConvertMapStringSlice
|
||||
}
|
||||
for k, v := range form {
|
||||
ptrMap[k] = v
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
ptrMap, ok := ptr.(map[string]string)
|
||||
if !ok {
|
||||
return ErrConvertToMapString
|
||||
}
|
||||
for k, v := range form {
|
||||
ptrMap[k] = v[len(v)-1] // pick last
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -1,67 +0,0 @@
|
|||
// Copyright 2019 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var form = map[string][]string{
|
||||
"name": {"mike"},
|
||||
"friends": {"anna", "nicole"},
|
||||
"id_number": {"12345678"},
|
||||
"id_date": {"2018-01-20"},
|
||||
}
|
||||
|
||||
type structFull struct {
|
||||
Name string `form:"name"`
|
||||
Age int `form:"age,default=25"`
|
||||
Friends []string `form:"friends"`
|
||||
ID *struct {
|
||||
Number string `form:"id_number"`
|
||||
DateOfIssue time.Time `form:"id_date" time_format:"2006-01-02" time_utc:"true"`
|
||||
}
|
||||
Nationality *string `form:"nationality"`
|
||||
}
|
||||
|
||||
func BenchmarkMapFormFull(b *testing.B) {
|
||||
var s structFull
|
||||
for i := 0; i < b.N; i++ {
|
||||
err := mapForm(&s, form)
|
||||
if err != nil {
|
||||
b.Fatalf("Error on a form mapping")
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
|
||||
t := b
|
||||
assert.Equal(t, "mike", s.Name)
|
||||
assert.Equal(t, 25, s.Age)
|
||||
assert.Equal(t, []string{"anna", "nicole"}, s.Friends)
|
||||
assert.Equal(t, "12345678", s.ID.Number)
|
||||
assert.Equal(t, time.Date(2018, 1, 20, 0, 0, 0, 0, time.UTC), s.ID.DateOfIssue)
|
||||
assert.Nil(t, s.Nationality)
|
||||
}
|
||||
|
||||
type structName struct {
|
||||
Name string `form:"name"`
|
||||
}
|
||||
|
||||
func BenchmarkMapFormName(b *testing.B) {
|
||||
var s structName
|
||||
for i := 0; i < b.N; i++ {
|
||||
err := mapForm(&s, form)
|
||||
if err != nil {
|
||||
b.Fatalf("Error on a form mapping")
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
|
||||
t := b
|
||||
assert.Equal(t, "mike", s.Name)
|
||||
}
|
|
@ -1,290 +0,0 @@
|
|||
// Copyright 2019 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMappingBaseTypes(t *testing.T) {
|
||||
intPtr := func(i int) *int {
|
||||
return &i
|
||||
}
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
value any
|
||||
form string
|
||||
expect any
|
||||
}{
|
||||
{"base type", struct{ F int }{}, "9", int(9)},
|
||||
{"base type", struct{ F int8 }{}, "9", int8(9)},
|
||||
{"base type", struct{ F int16 }{}, "9", int16(9)},
|
||||
{"base type", struct{ F int32 }{}, "9", int32(9)},
|
||||
{"base type", struct{ F int64 }{}, "9", int64(9)},
|
||||
{"base type", struct{ F uint }{}, "9", uint(9)},
|
||||
{"base type", struct{ F uint8 }{}, "9", uint8(9)},
|
||||
{"base type", struct{ F uint16 }{}, "9", uint16(9)},
|
||||
{"base type", struct{ F uint32 }{}, "9", uint32(9)},
|
||||
{"base type", struct{ F uint64 }{}, "9", uint64(9)},
|
||||
{"base type", struct{ F bool }{}, "True", true},
|
||||
{"base type", struct{ F float32 }{}, "9.1", float32(9.1)},
|
||||
{"base type", struct{ F float64 }{}, "9.1", float64(9.1)},
|
||||
{"base type", struct{ F string }{}, "test", string("test")},
|
||||
{"base type", struct{ F *int }{}, "9", intPtr(9)},
|
||||
|
||||
// zero values
|
||||
{"zero value", struct{ F int }{}, "", int(0)},
|
||||
{"zero value", struct{ F uint }{}, "", uint(0)},
|
||||
{"zero value", struct{ F bool }{}, "", false},
|
||||
{"zero value", struct{ F float32 }{}, "", float32(0)},
|
||||
} {
|
||||
tp := reflect.TypeOf(tt.value)
|
||||
testName := tt.name + ":" + tp.Field(0).Type.String()
|
||||
|
||||
val := reflect.New(reflect.TypeOf(tt.value))
|
||||
val.Elem().Set(reflect.ValueOf(tt.value))
|
||||
|
||||
field := val.Elem().Type().Field(0)
|
||||
|
||||
_, err := mapping(val, emptyField, formSource{field.Name: {tt.form}}, "form")
|
||||
assert.NoError(t, err, testName)
|
||||
|
||||
actual := val.Elem().Field(0).Interface()
|
||||
assert.Equal(t, tt.expect, actual, testName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMappingDefault(t *testing.T) {
|
||||
var s struct {
|
||||
Int int `form:",default=9"`
|
||||
Slice []int `form:",default=9"`
|
||||
Array [1]int `form:",default=9"`
|
||||
}
|
||||
err := mappingByPtr(&s, formSource{}, "form")
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, 9, s.Int)
|
||||
assert.Equal(t, []int{9}, s.Slice)
|
||||
assert.Equal(t, [1]int{9}, s.Array)
|
||||
}
|
||||
|
||||
func TestMappingSkipField(t *testing.T) {
|
||||
var s struct {
|
||||
A int
|
||||
}
|
||||
err := mappingByPtr(&s, formSource{}, "form")
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, 0, s.A)
|
||||
}
|
||||
|
||||
func TestMappingIgnoreField(t *testing.T) {
|
||||
var s struct {
|
||||
A int `form:"A"`
|
||||
B int `form:"-"`
|
||||
}
|
||||
err := mappingByPtr(&s, formSource{"A": {"9"}, "B": {"9"}}, "form")
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, 9, s.A)
|
||||
assert.Equal(t, 0, s.B)
|
||||
}
|
||||
|
||||
func TestMappingUnexportedField(t *testing.T) {
|
||||
var s struct {
|
||||
A int `form:"a"`
|
||||
b int `form:"b"`
|
||||
}
|
||||
err := mappingByPtr(&s, formSource{"a": {"9"}, "b": {"9"}}, "form")
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, 9, s.A)
|
||||
assert.Equal(t, 0, s.b)
|
||||
}
|
||||
|
||||
func TestMappingPrivateField(t *testing.T) {
|
||||
var s struct {
|
||||
f int `form:"field"`
|
||||
}
|
||||
err := mappingByPtr(&s, formSource{"field": {"6"}}, "form")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, s.f)
|
||||
}
|
||||
|
||||
func TestMappingUnknownFieldType(t *testing.T) {
|
||||
var s struct {
|
||||
U uintptr
|
||||
}
|
||||
|
||||
err := mappingByPtr(&s, formSource{"U": {"unknown"}}, "form")
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, errUnknownType, err)
|
||||
}
|
||||
|
||||
func TestMappingURI(t *testing.T) {
|
||||
var s struct {
|
||||
F int `uri:"field"`
|
||||
}
|
||||
err := mapURI(&s, map[string][]string{"field": {"6"}})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 6, s.F)
|
||||
}
|
||||
|
||||
func TestMappingForm(t *testing.T) {
|
||||
var s struct {
|
||||
F int `form:"field"`
|
||||
}
|
||||
err := mapForm(&s, map[string][]string{"field": {"6"}})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 6, s.F)
|
||||
}
|
||||
|
||||
func TestMapFormWithTag(t *testing.T) {
|
||||
var s struct {
|
||||
F int `externalTag:"field"`
|
||||
}
|
||||
err := MapFormWithTag(&s, map[string][]string{"field": {"6"}}, "externalTag")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 6, s.F)
|
||||
}
|
||||
|
||||
func TestMappingTime(t *testing.T) {
|
||||
var s struct {
|
||||
Time time.Time
|
||||
LocalTime time.Time `time_format:"2006-01-02"`
|
||||
ZeroValue time.Time
|
||||
CSTTime time.Time `time_format:"2006-01-02" time_location:"Asia/Shanghai"`
|
||||
UTCTime time.Time `time_format:"2006-01-02" time_utc:"1"`
|
||||
}
|
||||
|
||||
var err error
|
||||
time.Local, err = time.LoadLocation("Europe/Berlin")
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = mapForm(&s, map[string][]string{
|
||||
"Time": {"2019-01-20T16:02:58Z"},
|
||||
"LocalTime": {"2019-01-20"},
|
||||
"ZeroValue": {},
|
||||
"CSTTime": {"2019-01-20"},
|
||||
"UTCTime": {"2019-01-20"},
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "2019-01-20 16:02:58 +0000 UTC", s.Time.String())
|
||||
assert.Equal(t, "2019-01-20 00:00:00 +0100 CET", s.LocalTime.String())
|
||||
assert.Equal(t, "2019-01-19 23:00:00 +0000 UTC", s.LocalTime.UTC().String())
|
||||
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", s.ZeroValue.String())
|
||||
assert.Equal(t, "2019-01-20 00:00:00 +0800 CST", s.CSTTime.String())
|
||||
assert.Equal(t, "2019-01-19 16:00:00 +0000 UTC", s.CSTTime.UTC().String())
|
||||
assert.Equal(t, "2019-01-20 00:00:00 +0000 UTC", s.UTCTime.String())
|
||||
|
||||
// wrong location
|
||||
var wrongLoc struct {
|
||||
Time time.Time `time_location:"wrong"`
|
||||
}
|
||||
err = mapForm(&wrongLoc, map[string][]string{"Time": {"2019-01-20T16:02:58Z"}})
|
||||
assert.Error(t, err)
|
||||
|
||||
// wrong time value
|
||||
var wrongTime struct {
|
||||
Time time.Time
|
||||
}
|
||||
err = mapForm(&wrongTime, map[string][]string{"Time": {"wrong"}})
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestMappingTimeDuration(t *testing.T) {
|
||||
var s struct {
|
||||
D time.Duration
|
||||
}
|
||||
|
||||
// ok
|
||||
err := mappingByPtr(&s, formSource{"D": {"5s"}}, "form")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 5*time.Second, s.D)
|
||||
|
||||
// error
|
||||
err = mappingByPtr(&s, formSource{"D": {"wrong"}}, "form")
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestMappingSlice(t *testing.T) {
|
||||
var s struct {
|
||||
Slice []int `form:"slice,default=9"`
|
||||
}
|
||||
|
||||
// default value
|
||||
err := mappingByPtr(&s, formSource{}, "form")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []int{9}, s.Slice)
|
||||
|
||||
// ok
|
||||
err = mappingByPtr(&s, formSource{"slice": {"3", "4"}}, "form")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []int{3, 4}, s.Slice)
|
||||
|
||||
// error
|
||||
err = mappingByPtr(&s, formSource{"slice": {"wrong"}}, "form")
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestMappingArray(t *testing.T) {
|
||||
var s struct {
|
||||
Array [2]int `form:"array,default=9"`
|
||||
}
|
||||
|
||||
// wrong default
|
||||
err := mappingByPtr(&s, formSource{}, "form")
|
||||
assert.Error(t, err)
|
||||
|
||||
// ok
|
||||
err = mappingByPtr(&s, formSource{"array": {"3", "4"}}, "form")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, [2]int{3, 4}, s.Array)
|
||||
|
||||
// error - not enough vals
|
||||
err = mappingByPtr(&s, formSource{"array": {"3"}}, "form")
|
||||
assert.Error(t, err)
|
||||
|
||||
// error - wrong value
|
||||
err = mappingByPtr(&s, formSource{"array": {"wrong"}}, "form")
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestMappingStructField(t *testing.T) {
|
||||
var s struct {
|
||||
J struct {
|
||||
I int
|
||||
}
|
||||
}
|
||||
|
||||
err := mappingByPtr(&s, formSource{"J": {`{"I": 9}`}}, "form")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 9, s.J.I)
|
||||
}
|
||||
|
||||
func TestMappingMapField(t *testing.T) {
|
||||
var s struct {
|
||||
M map[string]int
|
||||
}
|
||||
|
||||
err := mappingByPtr(&s, formSource{"M": {`{"one": 1}`}}, "form")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, map[string]int{"one": 1}, s.M)
|
||||
}
|
||||
|
||||
func TestMappingIgnoredCircularRef(t *testing.T) {
|
||||
type S struct {
|
||||
S *S `form:"-"`
|
||||
}
|
||||
var s S
|
||||
|
||||
err := mappingByPtr(&s, formSource{}, "form")
|
||||
assert.NoError(t, err)
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
// Copyright 2022 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type headerBinding struct{}
|
||||
|
||||
func (headerBinding) Name() string {
|
||||
return "header"
|
||||
}
|
||||
|
||||
func (headerBinding) Bind(req *http.Request, obj any) error {
|
||||
|
||||
if err := mapHeader(obj, req.Header); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return validate(obj)
|
||||
}
|
||||
|
||||
func mapHeader(ptr any, h map[string][]string) error {
|
||||
return mappingByPtr(ptr, headerSource(h), "header")
|
||||
}
|
||||
|
||||
type headerSource map[string][]string
|
||||
|
||||
var _ setter = headerSource(nil)
|
||||
|
||||
func (hs headerSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt setOptions) (bool, error) {
|
||||
return setByForm(value, field, hs, textproto.CanonicalMIMEHeaderKey(tagValue), opt)
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"git.internal/re/gin/internal/json"
|
||||
)
|
||||
|
||||
// EnableDecoderUseNumber is used to call the UseNumber method on the JSON
|
||||
// Decoder instance. UseNumber causes the Decoder to unmarshal a number into an
|
||||
// interface{} as a Number instead of as a float64.
|
||||
var EnableDecoderUseNumber = false
|
||||
|
||||
// EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method
|
||||
// on the JSON Decoder instance. DisallowUnknownFields causes the Decoder to
|
||||
// return an error when the destination is a struct and the input contains object
|
||||
// keys which do not match any non-ignored, exported fields in the destination.
|
||||
var EnableDecoderDisallowUnknownFields = false
|
||||
|
||||
type jsonBinding struct{}
|
||||
|
||||
func (jsonBinding) Name() string {
|
||||
return "json"
|
||||
}
|
||||
|
||||
func (jsonBinding) Bind(req *http.Request, obj any) error {
|
||||
if req == nil || req.Body == nil {
|
||||
return errors.New("invalid request")
|
||||
}
|
||||
return decodeJSON(req.Body, obj)
|
||||
}
|
||||
|
||||
func (jsonBinding) BindBody(body []byte, obj any) error {
|
||||
return decodeJSON(bytes.NewReader(body), obj)
|
||||
}
|
||||
|
||||
func decodeJSON(r io.Reader, obj any) error {
|
||||
decoder := json.NewDecoder(r)
|
||||
if EnableDecoderUseNumber {
|
||||
decoder.UseNumber()
|
||||
}
|
||||
if EnableDecoderDisallowUnknownFields {
|
||||
decoder.DisallowUnknownFields()
|
||||
}
|
||||
if err := decoder.Decode(obj); err != nil {
|
||||
return err
|
||||
}
|
||||
return validate(obj)
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
// Copyright 2019 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestJSONBindingBindBody(t *testing.T) {
|
||||
var s struct {
|
||||
Foo string `json:"foo"`
|
||||
}
|
||||
err := jsonBinding{}.BindBody([]byte(`{"foo": "FOO"}`), &s)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "FOO", s.Foo)
|
||||
}
|
||||
|
||||
func TestJSONBindingBindBodyMap(t *testing.T) {
|
||||
s := make(map[string]string)
|
||||
err := jsonBinding{}.BindBody([]byte(`{"foo": "FOO","hello":"world"}`), &s)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, s, 2)
|
||||
assert.Equal(t, "FOO", s["foo"])
|
||||
assert.Equal(t, "world", s["hello"])
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !nomsgpack
|
||||
// +build !nomsgpack
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/ugorji/go/codec"
|
||||
)
|
||||
|
||||
type msgpackBinding struct{}
|
||||
|
||||
func (msgpackBinding) Name() string {
|
||||
return "msgpack"
|
||||
}
|
||||
|
||||
func (msgpackBinding) Bind(req *http.Request, obj any) error {
|
||||
return decodeMsgPack(req.Body, obj)
|
||||
}
|
||||
|
||||
func (msgpackBinding) BindBody(body []byte, obj any) error {
|
||||
return decodeMsgPack(bytes.NewReader(body), obj)
|
||||
}
|
||||
|
||||
func decodeMsgPack(r io.Reader, obj any) error {
|
||||
cdc := new(codec.MsgpackHandle)
|
||||
if err := codec.NewDecoder(r, cdc).Decode(&obj); err != nil {
|
||||
return err
|
||||
}
|
||||
return validate(obj)
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
// Copyright 2019 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !nomsgpack
|
||||
// +build !nomsgpack
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/ugorji/go/codec"
|
||||
)
|
||||
|
||||
func TestMsgpackBindingBindBody(t *testing.T) {
|
||||
type teststruct struct {
|
||||
Foo string `msgpack:"foo"`
|
||||
}
|
||||
var s teststruct
|
||||
err := msgpackBinding{}.BindBody(msgpackBody(t, teststruct{"FOO"}), &s)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "FOO", s.Foo)
|
||||
}
|
||||
|
||||
func msgpackBody(t *testing.T, obj any) []byte {
|
||||
var bs bytes.Buffer
|
||||
h := &codec.MsgpackHandle{}
|
||||
err := codec.NewEncoder(&bs, h).Encode(obj)
|
||||
require.NoError(t, err)
|
||||
return bs.Bytes()
|
||||
}
|
|
@ -1,74 +0,0 @@
|
|||
// Copyright 2019 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type multipartRequest http.Request
|
||||
|
||||
var _ setter = (*multipartRequest)(nil)
|
||||
|
||||
var (
|
||||
// ErrMultiFileHeader multipart.FileHeader invalid
|
||||
ErrMultiFileHeader = errors.New("unsupported field type for multipart.FileHeader")
|
||||
|
||||
// ErrMultiFileHeaderLenInvalid array for []*multipart.FileHeader len invalid
|
||||
ErrMultiFileHeaderLenInvalid = errors.New("unsupported len of array for []*multipart.FileHeader")
|
||||
)
|
||||
|
||||
// TrySet tries to set a value by the multipart request with the binding a form file
|
||||
func (r *multipartRequest) TrySet(value reflect.Value, field reflect.StructField, key string, opt setOptions) (bool, error) {
|
||||
if files := r.MultipartForm.File[key]; len(files) != 0 {
|
||||
return setByMultipartFormFile(value, field, files)
|
||||
}
|
||||
|
||||
return setByForm(value, field, r.MultipartForm.Value, key, opt)
|
||||
}
|
||||
|
||||
func setByMultipartFormFile(value reflect.Value, field reflect.StructField, files []*multipart.FileHeader) (isSet bool, err error) {
|
||||
switch value.Kind() {
|
||||
case reflect.Ptr:
|
||||
switch value.Interface().(type) {
|
||||
case *multipart.FileHeader:
|
||||
value.Set(reflect.ValueOf(files[0]))
|
||||
return true, nil
|
||||
}
|
||||
case reflect.Struct:
|
||||
switch value.Interface().(type) {
|
||||
case multipart.FileHeader:
|
||||
value.Set(reflect.ValueOf(*files[0]))
|
||||
return true, nil
|
||||
}
|
||||
case reflect.Slice:
|
||||
slice := reflect.MakeSlice(value.Type(), len(files), len(files))
|
||||
isSet, err = setArrayOfMultipartFormFiles(slice, field, files)
|
||||
if err != nil || !isSet {
|
||||
return isSet, err
|
||||
}
|
||||
value.Set(slice)
|
||||
return true, nil
|
||||
case reflect.Array:
|
||||
return setArrayOfMultipartFormFiles(value, field, files)
|
||||
}
|
||||
return false, ErrMultiFileHeader
|
||||
}
|
||||
|
||||
func setArrayOfMultipartFormFiles(value reflect.Value, field reflect.StructField, files []*multipart.FileHeader) (isSet bool, err error) {
|
||||
if value.Len() != len(files) {
|
||||
return false, ErrMultiFileHeaderLenInvalid
|
||||
}
|
||||
for i := range files {
|
||||
set, err := setByMultipartFormFile(value.Index(i), field, files[i:i+1])
|
||||
if err != nil || !set {
|
||||
return set, err
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
|
@ -1,138 +0,0 @@
|
|||
// Copyright 2019 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFormMultipartBindingBindOneFile(t *testing.T) {
|
||||
var s struct {
|
||||
FileValue multipart.FileHeader `form:"file"`
|
||||
FilePtr *multipart.FileHeader `form:"file"`
|
||||
SliceValues []multipart.FileHeader `form:"file"`
|
||||
SlicePtrs []*multipart.FileHeader `form:"file"`
|
||||
ArrayValues [1]multipart.FileHeader `form:"file"`
|
||||
ArrayPtrs [1]*multipart.FileHeader `form:"file"`
|
||||
}
|
||||
file := testFile{"file", "file1", []byte("hello")}
|
||||
|
||||
req := createRequestMultipartFiles(t, file)
|
||||
err := FormMultipart.Bind(req, &s)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assertMultipartFileHeader(t, &s.FileValue, file)
|
||||
assertMultipartFileHeader(t, s.FilePtr, file)
|
||||
assert.Len(t, s.SliceValues, 1)
|
||||
assertMultipartFileHeader(t, &s.SliceValues[0], file)
|
||||
assert.Len(t, s.SlicePtrs, 1)
|
||||
assertMultipartFileHeader(t, s.SlicePtrs[0], file)
|
||||
assertMultipartFileHeader(t, &s.ArrayValues[0], file)
|
||||
assertMultipartFileHeader(t, s.ArrayPtrs[0], file)
|
||||
}
|
||||
|
||||
func TestFormMultipartBindingBindTwoFiles(t *testing.T) {
|
||||
var s struct {
|
||||
SliceValues []multipart.FileHeader `form:"file"`
|
||||
SlicePtrs []*multipart.FileHeader `form:"file"`
|
||||
ArrayValues [2]multipart.FileHeader `form:"file"`
|
||||
ArrayPtrs [2]*multipart.FileHeader `form:"file"`
|
||||
}
|
||||
files := []testFile{
|
||||
{"file", "file1", []byte("hello")},
|
||||
{"file", "file2", []byte("world")},
|
||||
}
|
||||
|
||||
req := createRequestMultipartFiles(t, files...)
|
||||
err := FormMultipart.Bind(req, &s)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Len(t, s.SliceValues, len(files))
|
||||
assert.Len(t, s.SlicePtrs, len(files))
|
||||
assert.Len(t, s.ArrayValues, len(files))
|
||||
assert.Len(t, s.ArrayPtrs, len(files))
|
||||
|
||||
for i, file := range files {
|
||||
assertMultipartFileHeader(t, &s.SliceValues[i], file)
|
||||
assertMultipartFileHeader(t, s.SlicePtrs[i], file)
|
||||
assertMultipartFileHeader(t, &s.ArrayValues[i], file)
|
||||
assertMultipartFileHeader(t, s.ArrayPtrs[i], file)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormMultipartBindingBindError(t *testing.T) {
|
||||
files := []testFile{
|
||||
{"file", "file1", []byte("hello")},
|
||||
{"file", "file2", []byte("world")},
|
||||
}
|
||||
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
s any
|
||||
}{
|
||||
{"wrong type", &struct {
|
||||
Files int `form:"file"`
|
||||
}{}},
|
||||
{"wrong array size", &struct {
|
||||
Files [1]*multipart.FileHeader `form:"file"`
|
||||
}{}},
|
||||
{"wrong slice type", &struct {
|
||||
Files []int `form:"file"`
|
||||
}{}},
|
||||
} {
|
||||
req := createRequestMultipartFiles(t, files...)
|
||||
err := FormMultipart.Bind(req, tt.s)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
type testFile struct {
|
||||
Fieldname string
|
||||
Filename string
|
||||
Content []byte
|
||||
}
|
||||
|
||||
func createRequestMultipartFiles(t *testing.T, files ...testFile) *http.Request {
|
||||
var body bytes.Buffer
|
||||
|
||||
mw := multipart.NewWriter(&body)
|
||||
for _, file := range files {
|
||||
fw, err := mw.CreateFormFile(file.Fieldname, file.Filename)
|
||||
assert.NoError(t, err)
|
||||
|
||||
n, err := fw.Write(file.Content)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, len(file.Content), n)
|
||||
}
|
||||
err := mw.Close()
|
||||
assert.NoError(t, err)
|
||||
|
||||
req, err := http.NewRequest("POST", "/", &body)
|
||||
assert.NoError(t, err)
|
||||
|
||||
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+mw.Boundary())
|
||||
return req
|
||||
}
|
||||
|
||||
func assertMultipartFileHeader(t *testing.T, fh *multipart.FileHeader, file testFile) {
|
||||
assert.Equal(t, file.Filename, fh.Filename)
|
||||
assert.Equal(t, int64(len(file.Content)), fh.Size)
|
||||
|
||||
fl, err := fh.Open()
|
||||
assert.NoError(t, err)
|
||||
|
||||
body, err := io.ReadAll(fl)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, string(file.Content), string(body))
|
||||
|
||||
err = fl.Close()
|
||||
assert.NoError(t, err)
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type protobufBinding struct{}
|
||||
|
||||
func (protobufBinding) Name() string {
|
||||
return "protobuf"
|
||||
}
|
||||
|
||||
func (b protobufBinding) Bind(req *http.Request, obj any) error {
|
||||
buf, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return b.BindBody(buf, obj)
|
||||
}
|
||||
|
||||
func (protobufBinding) BindBody(body []byte, obj any) error {
|
||||
msg, ok := obj.(proto.Message)
|
||||
if !ok {
|
||||
return errors.New("obj is not ProtoMessage")
|
||||
}
|
||||
if err := proto.Unmarshal(body, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
// Here it's same to return validate(obj), but util now we can't add
|
||||
// `binding:""` to the struct which automatically generate by gen-proto
|
||||
return nil
|
||||
// return validate(obj)
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import "net/http"
|
||||
|
||||
type queryBinding struct{}
|
||||
|
||||
func (queryBinding) Name() string {
|
||||
return "query"
|
||||
}
|
||||
|
||||
func (queryBinding) Bind(req *http.Request, obj any) error {
|
||||
values := req.URL.Query()
|
||||
if err := mapForm(obj, values); err != nil {
|
||||
return err
|
||||
}
|
||||
return validate(obj)
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
// Copyright 2022 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
)
|
||||
|
||||
type tomlBinding struct{}
|
||||
|
||||
func (tomlBinding) Name() string {
|
||||
return "toml"
|
||||
}
|
||||
|
||||
func decodeToml(r io.Reader, obj any) error {
|
||||
decoder := toml.NewDecoder(r)
|
||||
if err := decoder.Decode(obj); err != nil {
|
||||
return err
|
||||
}
|
||||
return decoder.Decode(obj)
|
||||
}
|
||||
|
||||
func (tomlBinding) Bind(req *http.Request, obj any) error {
|
||||
return decodeToml(req.Body, obj)
|
||||
}
|
||||
|
||||
func (tomlBinding) BindBody(body []byte, obj any) error {
|
||||
return decodeToml(bytes.NewReader(body), obj)
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
// Copyright 2022 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTOMLBindingBindBody(t *testing.T) {
|
||||
var s struct {
|
||||
Foo string `toml:"foo"`
|
||||
}
|
||||
tomlBody := `foo="FOO"`
|
||||
err := tomlBinding{}.BindBody([]byte(tomlBody), &s)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "FOO", s.Foo)
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
// Copyright 2018 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
type uriBinding struct{}
|
||||
|
||||
func (uriBinding) Name() string {
|
||||
return "uri"
|
||||
}
|
||||
|
||||
func (uriBinding) BindUri(m map[string][]string, obj any) error {
|
||||
if err := mapURI(obj, m); err != nil {
|
||||
return err
|
||||
}
|
||||
return validate(obj)
|
||||
}
|
|
@ -1,228 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type testInterface interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
type substructNoValidation struct {
|
||||
IString string
|
||||
IInt int
|
||||
}
|
||||
|
||||
type mapNoValidationSub map[string]substructNoValidation
|
||||
|
||||
type structNoValidationValues struct {
|
||||
substructNoValidation
|
||||
|
||||
Boolean bool
|
||||
|
||||
Uinteger uint
|
||||
Integer int
|
||||
Integer8 int8
|
||||
Integer16 int16
|
||||
Integer32 int32
|
||||
Integer64 int64
|
||||
Uinteger8 uint8
|
||||
Uinteger16 uint16
|
||||
Uinteger32 uint32
|
||||
Uinteger64 uint64
|
||||
|
||||
Float32 float32
|
||||
Float64 float64
|
||||
|
||||
String string
|
||||
|
||||
Date time.Time
|
||||
|
||||
Struct substructNoValidation
|
||||
InlinedStruct struct {
|
||||
String []string
|
||||
Integer int
|
||||
}
|
||||
|
||||
IntSlice []int
|
||||
IntPointerSlice []*int
|
||||
StructPointerSlice []*substructNoValidation
|
||||
StructSlice []substructNoValidation
|
||||
InterfaceSlice []testInterface
|
||||
|
||||
UniversalInterface any
|
||||
CustomInterface testInterface
|
||||
|
||||
FloatMap map[string]float32
|
||||
StructMap mapNoValidationSub
|
||||
}
|
||||
|
||||
func createNoValidationValues() structNoValidationValues {
|
||||
integer := 1
|
||||
s := structNoValidationValues{
|
||||
Boolean: true,
|
||||
Uinteger: 1 << 29,
|
||||
Integer: -10000,
|
||||
Integer8: 120,
|
||||
Integer16: -20000,
|
||||
Integer32: 1 << 29,
|
||||
Integer64: 1 << 61,
|
||||
Uinteger8: 250,
|
||||
Uinteger16: 50000,
|
||||
Uinteger32: 1 << 31,
|
||||
Uinteger64: 1 << 62,
|
||||
Float32: 123.456,
|
||||
Float64: 123.456789,
|
||||
String: "text",
|
||||
Date: time.Time{},
|
||||
CustomInterface: &bytes.Buffer{},
|
||||
Struct: substructNoValidation{},
|
||||
IntSlice: []int{-3, -2, 1, 0, 1, 2, 3},
|
||||
IntPointerSlice: []*int{&integer},
|
||||
StructSlice: []substructNoValidation{},
|
||||
UniversalInterface: 1.2,
|
||||
FloatMap: map[string]float32{
|
||||
"foo": 1.23,
|
||||
"bar": 232.323,
|
||||
},
|
||||
StructMap: mapNoValidationSub{
|
||||
"foo": substructNoValidation{},
|
||||
"bar": substructNoValidation{},
|
||||
},
|
||||
// StructPointerSlice []noValidationSub
|
||||
// InterfaceSlice []testInterface
|
||||
}
|
||||
s.InlinedStruct.Integer = 1000
|
||||
s.InlinedStruct.String = []string{"first", "second"}
|
||||
s.IString = "substring"
|
||||
s.IInt = 987654
|
||||
return s
|
||||
}
|
||||
|
||||
func TestValidateNoValidationValues(t *testing.T) {
|
||||
origin := createNoValidationValues()
|
||||
test := createNoValidationValues()
|
||||
empty := structNoValidationValues{}
|
||||
|
||||
assert.Nil(t, validate(test))
|
||||
assert.Nil(t, validate(&test))
|
||||
assert.Nil(t, validate(empty))
|
||||
assert.Nil(t, validate(&empty))
|
||||
|
||||
assert.Equal(t, origin, test)
|
||||
}
|
||||
|
||||
type structNoValidationPointer struct {
|
||||
substructNoValidation
|
||||
|
||||
Boolean bool
|
||||
|
||||
Uinteger *uint
|
||||
Integer *int
|
||||
Integer8 *int8
|
||||
Integer16 *int16
|
||||
Integer32 *int32
|
||||
Integer64 *int64
|
||||
Uinteger8 *uint8
|
||||
Uinteger16 *uint16
|
||||
Uinteger32 *uint32
|
||||
Uinteger64 *uint64
|
||||
|
||||
Float32 *float32
|
||||
Float64 *float64
|
||||
|
||||
String *string
|
||||
|
||||
Date *time.Time
|
||||
|
||||
Struct *substructNoValidation
|
||||
|
||||
IntSlice *[]int
|
||||
IntPointerSlice *[]*int
|
||||
StructPointerSlice *[]*substructNoValidation
|
||||
StructSlice *[]substructNoValidation
|
||||
InterfaceSlice *[]testInterface
|
||||
|
||||
FloatMap *map[string]float32
|
||||
StructMap *mapNoValidationSub
|
||||
}
|
||||
|
||||
func TestValidateNoValidationPointers(t *testing.T) {
|
||||
//origin := createNoValidation_values()
|
||||
//test := createNoValidation_values()
|
||||
empty := structNoValidationPointer{}
|
||||
|
||||
//assert.Nil(t, validate(test))
|
||||
//assert.Nil(t, validate(&test))
|
||||
assert.Nil(t, validate(empty))
|
||||
assert.Nil(t, validate(&empty))
|
||||
|
||||
//assert.Equal(t, origin, test)
|
||||
}
|
||||
|
||||
type Object map[string]any
|
||||
|
||||
func TestValidatePrimitives(t *testing.T) {
|
||||
obj := Object{"foo": "bar", "bar": 1}
|
||||
assert.NoError(t, validate(obj))
|
||||
assert.NoError(t, validate(&obj))
|
||||
assert.Equal(t, Object{"foo": "bar", "bar": 1}, obj)
|
||||
|
||||
obj2 := []Object{{"foo": "bar", "bar": 1}, {"foo": "bar", "bar": 1}}
|
||||
assert.NoError(t, validate(obj2))
|
||||
assert.NoError(t, validate(&obj2))
|
||||
|
||||
nu := 10
|
||||
assert.NoError(t, validate(nu))
|
||||
assert.NoError(t, validate(&nu))
|
||||
assert.Equal(t, 10, nu)
|
||||
|
||||
str := "value"
|
||||
assert.NoError(t, validate(str))
|
||||
assert.NoError(t, validate(&str))
|
||||
assert.Equal(t, "value", str)
|
||||
}
|
||||
|
||||
// structCustomValidation is a helper struct we use to check that
|
||||
// custom validation can be registered on it.
|
||||
// The `notone` binding directive is for custom validation and registered later.
|
||||
type structCustomValidation struct {
|
||||
Integer int `binding:"notone"`
|
||||
}
|
||||
|
||||
func notOne(f1 validator.FieldLevel) bool {
|
||||
if val, ok := f1.Field().Interface().(int); ok {
|
||||
return val != 1
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func TestValidatorEngine(t *testing.T) {
|
||||
// This validates that the function `notOne` matches
|
||||
// the expected function signature by `defaultValidator`
|
||||
// and by extension the validator library.
|
||||
engine, ok := Validator.Engine().(*validator.Validate)
|
||||
assert.True(t, ok)
|
||||
|
||||
err := engine.RegisterValidation("notone", notOne)
|
||||
// Check that we can register custom validation without error
|
||||
assert.Nil(t, err)
|
||||
|
||||
// Create an instance which will fail validation
|
||||
withOne := structCustomValidation{Integer: 1}
|
||||
errs := validate(withOne)
|
||||
|
||||
// Check that we got back non-nil errs
|
||||
assert.NotNil(t, errs)
|
||||
// Check that the error matches expectation
|
||||
assert.Error(t, errs, "", "", "notone")
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type xmlBinding struct{}
|
||||
|
||||
func (xmlBinding) Name() string {
|
||||
return "xml"
|
||||
}
|
||||
|
||||
func (xmlBinding) Bind(req *http.Request, obj any) error {
|
||||
return decodeXML(req.Body, obj)
|
||||
}
|
||||
|
||||
func (xmlBinding) BindBody(body []byte, obj any) error {
|
||||
return decodeXML(bytes.NewReader(body), obj)
|
||||
}
|
||||
func decodeXML(r io.Reader, obj any) error {
|
||||
decoder := xml.NewDecoder(r)
|
||||
if err := decoder.Decode(obj); err != nil {
|
||||
return err
|
||||
}
|
||||
return validate(obj)
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
// Copyright 2019 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestXMLBindingBindBody(t *testing.T) {
|
||||
var s struct {
|
||||
Foo string `xml:"foo"`
|
||||
}
|
||||
xmlBody := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<root>
|
||||
<foo>FOO</foo>
|
||||
</root>`
|
||||
err := xmlBinding{}.BindBody([]byte(xmlBody), &s)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "FOO", s.Foo)
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
// Copyright 2018 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type yamlBinding struct{}
|
||||
|
||||
func (yamlBinding) Name() string {
|
||||
return "yaml"
|
||||
}
|
||||
|
||||
func (yamlBinding) Bind(req *http.Request, obj any) error {
|
||||
return decodeYAML(req.Body, obj)
|
||||
}
|
||||
|
||||
func (yamlBinding) BindBody(body []byte, obj any) error {
|
||||
return decodeYAML(bytes.NewReader(body), obj)
|
||||
}
|
||||
|
||||
func decodeYAML(r io.Reader, obj any) error {
|
||||
decoder := yaml.NewDecoder(r)
|
||||
if err := decoder.Decode(obj); err != nil {
|
||||
return err
|
||||
}
|
||||
return validate(obj)
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
// Copyright 2019 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestYAMLBindingBindBody(t *testing.T) {
|
||||
var s struct {
|
||||
Foo string `yaml:"foo"`
|
||||
}
|
||||
err := yamlBinding{}.BindBody([]byte("foo: FOO"), &s)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "FOO", s.Foo)
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
coverage:
|
||||
notify:
|
||||
gitter:
|
||||
default:
|
||||
url: https://webhooks.gitter.im/e/d90dcdeeab2f1e357165
|
1218
context.go
1218
context.go
File diff suppressed because it is too large
Load Diff
|
@ -1,31 +0,0 @@
|
|||
// Copyright 2021 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !go1.17
|
||||
// +build !go1.17
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestContextFormFileFailed16(t *testing.T) {
|
||||
buf := new(bytes.Buffer)
|
||||
mw := multipart.NewWriter(buf)
|
||||
mw.Close()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
|
||||
c.engine.MaxMultipartMemory = 8 << 20
|
||||
f, err := c.FormFile("file")
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, f)
|
||||
}
|
|
@ -1,94 +0,0 @@
|
|||
// Copyright 2021 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build go1.17
|
||||
// +build go1.17
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type interceptedWriter struct {
|
||||
ResponseWriter
|
||||
b *bytes.Buffer
|
||||
}
|
||||
|
||||
func (i interceptedWriter) WriteHeader(code int) {
|
||||
i.Header().Del("X-Test")
|
||||
i.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
func TestContextFormFileFailed17(t *testing.T) {
|
||||
if !isGo117OrGo118() {
|
||||
return
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
mw := multipart.NewWriter(buf)
|
||||
defer func(mw *multipart.Writer) {
|
||||
err := mw.Close()
|
||||
if err != nil {
|
||||
assert.Error(t, err)
|
||||
}
|
||||
}(mw)
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
|
||||
c.engine.MaxMultipartMemory = 8 << 20
|
||||
assert.Panics(t, func() {
|
||||
f, err := c.FormFile("file")
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, f)
|
||||
})
|
||||
}
|
||||
|
||||
func TestInterceptedHeader(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, r := CreateTestContext(w)
|
||||
|
||||
r.Use(func(c *Context) {
|
||||
i := interceptedWriter{
|
||||
ResponseWriter: c.Writer,
|
||||
b: bytes.NewBuffer(nil),
|
||||
}
|
||||
c.Writer = i
|
||||
c.Next()
|
||||
c.Header("X-Test", "overridden")
|
||||
c.Writer = i.ResponseWriter
|
||||
})
|
||||
r.GET("/", func(c *Context) {
|
||||
c.Header("X-Test", "original")
|
||||
c.Header("X-Test-2", "present")
|
||||
c.String(http.StatusOK, "hello world")
|
||||
})
|
||||
c.Request = httptest.NewRequest("GET", "/", nil)
|
||||
r.HandleContext(c)
|
||||
// Result() has headers frozen when WriteHeaderNow() has been called
|
||||
// Compared to this time, this is when the response headers will be flushed
|
||||
// As response is flushed on c.String, the Header cannot be set by the first
|
||||
// middleware. Assert this
|
||||
assert.Equal(t, "", w.Result().Header.Get("X-Test"))
|
||||
assert.Equal(t, "present", w.Result().Header.Get("X-Test-2"))
|
||||
}
|
||||
|
||||
func isGo117OrGo118() bool {
|
||||
version := strings.Split(runtime.Version()[2:], ".")
|
||||
if len(version) >= 2 {
|
||||
x := version[0]
|
||||
y := version[1]
|
||||
if x == "1" && (y == "17" || y == "18") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
// Copyright 2022 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build go1.19
|
||||
// +build go1.19
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestContextFormFileFailed19(t *testing.T) {
|
||||
buf := new(bytes.Buffer)
|
||||
mw := multipart.NewWriter(buf)
|
||||
mw.Close()
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
|
||||
c.engine.MaxMultipartMemory = 8 << 20
|
||||
f, err := c.FormFile("file")
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, f)
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build appengine
|
||||
// +build appengine
|
||||
|
||||
package gin
|
||||
|
||||
func init() {
|
||||
defaultPlatform = PlatformGoogleAppEngine
|
||||
}
|
2370
context_test.go
2370
context_test.go
File diff suppressed because it is too large
Load Diff
101
debug.go
101
debug.go
|
@ -1,101 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const ginSupportMinGoVer = 16
|
||||
|
||||
// IsDebugging returns true if the framework is running in debug mode.
|
||||
// Use SetMode(gin.ReleaseMode) to disable debug mode.
|
||||
func IsDebugging() bool {
|
||||
return ginMode == debugCode
|
||||
}
|
||||
|
||||
// DebugPrintRouteFunc indicates debug log output format.
|
||||
var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)
|
||||
|
||||
func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
|
||||
if IsDebugging() {
|
||||
nuHandlers := len(handlers)
|
||||
handlerName := nameOfFunction(handlers.Last())
|
||||
if DebugPrintRouteFunc == nil {
|
||||
debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
|
||||
} else {
|
||||
DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func debugPrintLoadTemplate(tmpl *template.Template) {
|
||||
if IsDebugging() {
|
||||
var buf strings.Builder
|
||||
for _, tmpl := range tmpl.Templates() {
|
||||
buf.WriteString("\t- ")
|
||||
buf.WriteString(tmpl.Name())
|
||||
buf.WriteString("\n")
|
||||
}
|
||||
debugPrint("Loaded HTML Templates (%d): \n%s\n", len(tmpl.Templates()), buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func debugPrint(format string, values ...any) {
|
||||
if IsDebugging() {
|
||||
if !strings.HasSuffix(format, "\n") {
|
||||
format += "\n"
|
||||
}
|
||||
fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...)
|
||||
}
|
||||
}
|
||||
|
||||
func getMinVer(v string) (uint64, error) {
|
||||
first := strings.IndexByte(v, '.')
|
||||
last := strings.LastIndexByte(v, '.')
|
||||
if first == last {
|
||||
return strconv.ParseUint(v[first+1:], 10, 64)
|
||||
}
|
||||
return strconv.ParseUint(v[first+1:last], 10, 64)
|
||||
}
|
||||
|
||||
func debugPrintWARNINGDefault() {
|
||||
if v, e := getMinVer(runtime.Version()); e == nil && v < ginSupportMinGoVer {
|
||||
debugPrint(`[WARNING] Now Gin requires Go 1.16+.
|
||||
|
||||
`)
|
||||
}
|
||||
debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
|
||||
|
||||
`)
|
||||
}
|
||||
|
||||
func debugPrintWARNINGNew() {
|
||||
debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production.
|
||||
- using env: export GIN_MODE=release
|
||||
- using code: gin.SetMode(gin.ReleaseMode)
|
||||
|
||||
`)
|
||||
}
|
||||
|
||||
func debugPrintWARNINGSetHTMLTemplate() {
|
||||
debugPrint(`[WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called
|
||||
at initialization. ie. before any route is registered or the router is listening in a socket:
|
||||
|
||||
router := gin.Default()
|
||||
router.SetHTMLTemplate(template) // << good place
|
||||
|
||||
`)
|
||||
}
|
||||
|
||||
func debugPrintError(err error) {
|
||||
if err != nil && IsDebugging() {
|
||||
fmt.Fprintf(DefaultErrorWriter, "[GIN-debug] [ERROR] %v\n", err)
|
||||
}
|
||||
}
|
166
debug_test.go
166
debug_test.go
|
@ -1,166 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TODO
|
||||
// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
|
||||
// func debugPrint(format string, values ...interface{}) {
|
||||
|
||||
func TestIsDebugging(t *testing.T) {
|
||||
SetMode(DebugMode)
|
||||
assert.True(t, IsDebugging())
|
||||
SetMode(ReleaseMode)
|
||||
assert.False(t, IsDebugging())
|
||||
SetMode(TestMode)
|
||||
assert.False(t, IsDebugging())
|
||||
}
|
||||
|
||||
func TestDebugPrint(t *testing.T) {
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
SetMode(ReleaseMode)
|
||||
debugPrint("DEBUG this!")
|
||||
SetMode(TestMode)
|
||||
debugPrint("DEBUG this!")
|
||||
SetMode(DebugMode)
|
||||
debugPrint("these are %d %s", 2, "error messages")
|
||||
SetMode(TestMode)
|
||||
})
|
||||
assert.Equal(t, "[GIN-debug] these are 2 error messages\n", re)
|
||||
}
|
||||
|
||||
func TestDebugPrintError(t *testing.T) {
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
debugPrintError(nil)
|
||||
debugPrintError(errors.New("this is an error"))
|
||||
SetMode(TestMode)
|
||||
})
|
||||
assert.Equal(t, "[GIN-debug] [ERROR] this is an error\n", re)
|
||||
}
|
||||
|
||||
func TestDebugPrintRoutes(t *testing.T) {
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
debugPrintRoute("GET", "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest})
|
||||
SetMode(TestMode)
|
||||
})
|
||||
assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param --> (.*/vendor/)?git.internal/re/gin.handlerNameTest \(2 handlers\)\n$`, re)
|
||||
}
|
||||
|
||||
func TestDebugPrintRouteFunc(t *testing.T) {
|
||||
DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
|
||||
fmt.Fprintf(DefaultWriter, "[GIN-debug] %-6s %-40s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
|
||||
}
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
debugPrintRoute("GET", "/path/to/route/:param1/:param2", HandlersChain{func(c *Context) {}, handlerNameTest})
|
||||
SetMode(TestMode)
|
||||
})
|
||||
assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param1/:param2 --> (.*/vendor/)?git.internal/re/gin.handlerNameTest \(2 handlers\)\n$`, re)
|
||||
}
|
||||
|
||||
func TestDebugPrintLoadTemplate(t *testing.T) {
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
templ := template.Must(template.New("").Delims("{[{", "}]}").ParseGlob("./testdata/template/hello.tmpl"))
|
||||
debugPrintLoadTemplate(templ)
|
||||
SetMode(TestMode)
|
||||
})
|
||||
assert.Regexp(t, `^\[GIN-debug\] Loaded HTML Templates \(2\): \n(\t- \n|\t- hello\.tmpl\n){2}\n`, re)
|
||||
}
|
||||
|
||||
func TestDebugPrintWARNINGSetHTMLTemplate(t *testing.T) {
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
debugPrintWARNINGSetHTMLTemplate()
|
||||
SetMode(TestMode)
|
||||
})
|
||||
assert.Equal(t, "[GIN-debug] [WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called\nat initialization. ie. before any route is registered or the router is listening in a socket:\n\n\trouter := gin.Default()\n\trouter.SetHTMLTemplate(template) // << good place\n\n", re)
|
||||
}
|
||||
|
||||
func TestDebugPrintWARNINGDefault(t *testing.T) {
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
debugPrintWARNINGDefault()
|
||||
SetMode(TestMode)
|
||||
})
|
||||
m, e := getMinVer(runtime.Version())
|
||||
if e == nil && m < ginSupportMinGoVer {
|
||||
assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.16+.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
|
||||
} else {
|
||||
assert.Equal(t, "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDebugPrintWARNINGNew(t *testing.T) {
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
debugPrintWARNINGNew()
|
||||
SetMode(TestMode)
|
||||
})
|
||||
assert.Equal(t, "[GIN-debug] [WARNING] Running in \"debug\" mode. Switch to \"release\" mode in production.\n - using env:\texport GIN_MODE=release\n - using code:\tgin.SetMode(gin.ReleaseMode)\n\n", re)
|
||||
}
|
||||
|
||||
func captureOutput(t *testing.T, f func()) string {
|
||||
reader, writer, err := os.Pipe()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defaultWriter := DefaultWriter
|
||||
defaultErrorWriter := DefaultErrorWriter
|
||||
defer func() {
|
||||
DefaultWriter = defaultWriter
|
||||
DefaultErrorWriter = defaultErrorWriter
|
||||
log.SetOutput(os.Stderr)
|
||||
}()
|
||||
DefaultWriter = writer
|
||||
DefaultErrorWriter = writer
|
||||
log.SetOutput(writer)
|
||||
out := make(chan string)
|
||||
wg := new(sync.WaitGroup)
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
var buf bytes.Buffer
|
||||
wg.Done()
|
||||
_, err := io.Copy(&buf, reader)
|
||||
assert.NoError(t, err)
|
||||
out <- buf.String()
|
||||
}()
|
||||
wg.Wait()
|
||||
f()
|
||||
writer.Close()
|
||||
return <-out
|
||||
}
|
||||
|
||||
func TestGetMinVer(t *testing.T) {
|
||||
var m uint64
|
||||
var e error
|
||||
_, e = getMinVer("go1")
|
||||
assert.NotNil(t, e)
|
||||
m, e = getMinVer("go1.1")
|
||||
assert.Equal(t, uint64(1), m)
|
||||
assert.Nil(t, e)
|
||||
m, e = getMinVer("go1.1.1")
|
||||
assert.Nil(t, e)
|
||||
assert.Equal(t, uint64(1), m)
|
||||
_, e = getMinVer("go1.1.1.1")
|
||||
assert.NotNil(t, e)
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"git.internal/re/gin/binding"
|
||||
)
|
||||
|
||||
// BindWith binds the passed struct pointer using the specified binding engine.
|
||||
// See the binding package.
|
||||
func (c *Context) BindWith(obj any, b binding.Binding) error {
|
||||
log.Println(`BindWith(\"interface{}, binding.Binding\") error is going to
|
||||
be deprecated, please check issue #662 and either use MustBindWith() if you
|
||||
want HTTP 400 to be automatically returned if any error occur, or use
|
||||
ShouldBindWith() if you need to manage the error.`)
|
||||
return c.MustBindWith(obj, b)
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"git.internal/re/gin/binding"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBindWith(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.Request, _ = http.NewRequest("POST", "/?foo=bar&bar=foo", bytes.NewBufferString("foo=unused"))
|
||||
|
||||
var obj struct {
|
||||
Foo string `form:"foo"`
|
||||
Bar string `form:"bar"`
|
||||
}
|
||||
captureOutput(t, func() {
|
||||
assert.NoError(t, c.BindWith(&obj, binding.Form))
|
||||
})
|
||||
assert.Equal(t, "foo", obj.Bar)
|
||||
assert.Equal(t, "bar", obj.Foo)
|
||||
assert.Equal(t, 0, w.Body.Len())
|
||||
}
|
6
doc.go
6
doc.go
|
@ -1,6 +0,0 @@
|
|||
/*
|
||||
Package gin implements a HTTP web framework called gin.
|
||||
|
||||
See https://gin-gonic.com/ for more information about gin.
|
||||
*/
|
||||
package gin // import "git.internal/re/gin"
|
2243
docs/doc.md
2243
docs/doc.md
File diff suppressed because it is too large
Load Diff
175
errors.go
175
errors.go
|
@ -1,175 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"git.internal/re/gin/internal/json"
|
||||
)
|
||||
|
||||
// ErrorType is an unsigned 64-bit error code as defined in the gin spec.
|
||||
type ErrorType uint64
|
||||
|
||||
const (
|
||||
// ErrorTypeBind is used when Context.Bind() fails.
|
||||
ErrorTypeBind ErrorType = 1 << 63
|
||||
// ErrorTypeRender is used when Context.Render() fails.
|
||||
ErrorTypeRender ErrorType = 1 << 62
|
||||
// ErrorTypePrivate indicates a private error.
|
||||
ErrorTypePrivate ErrorType = 1 << 0
|
||||
// ErrorTypePublic indicates a public error.
|
||||
ErrorTypePublic ErrorType = 1 << 1
|
||||
// ErrorTypeAny indicates any other error.
|
||||
ErrorTypeAny ErrorType = 1<<64 - 1
|
||||
// ErrorTypeNu indicates any other error.
|
||||
ErrorTypeNu = 2
|
||||
)
|
||||
|
||||
// Error represents a error's specification.
|
||||
type Error struct {
|
||||
Err error
|
||||
Type ErrorType
|
||||
Meta any
|
||||
}
|
||||
|
||||
type errorMsgs []*Error
|
||||
|
||||
var _ error = (*Error)(nil)
|
||||
|
||||
// SetType sets the error's type.
|
||||
func (msg *Error) SetType(flags ErrorType) *Error {
|
||||
msg.Type = flags
|
||||
return msg
|
||||
}
|
||||
|
||||
// SetMeta sets the error's meta data.
|
||||
func (msg *Error) SetMeta(data any) *Error {
|
||||
msg.Meta = data
|
||||
return msg
|
||||
}
|
||||
|
||||
// JSON creates a properly formatted JSON
|
||||
func (msg *Error) JSON() any {
|
||||
jsonData := H{}
|
||||
if msg.Meta != nil {
|
||||
value := reflect.ValueOf(msg.Meta)
|
||||
switch value.Kind() {
|
||||
case reflect.Struct:
|
||||
return msg.Meta
|
||||
case reflect.Map:
|
||||
for _, key := range value.MapKeys() {
|
||||
jsonData[key.String()] = value.MapIndex(key).Interface()
|
||||
}
|
||||
default:
|
||||
jsonData["meta"] = msg.Meta
|
||||
}
|
||||
}
|
||||
if _, ok := jsonData["error"]; !ok {
|
||||
jsonData["error"] = msg.Error()
|
||||
}
|
||||
return jsonData
|
||||
}
|
||||
|
||||
// MarshalJSON implements the json.Marshaller interface.
|
||||
func (msg *Error) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(msg.JSON())
|
||||
}
|
||||
|
||||
// Error implements the error interface.
|
||||
func (msg Error) Error() string {
|
||||
return msg.Err.Error()
|
||||
}
|
||||
|
||||
// IsType judges one error.
|
||||
func (msg *Error) IsType(flags ErrorType) bool {
|
||||
return (msg.Type & flags) > 0
|
||||
}
|
||||
|
||||
// Unwrap returns the wrapped error, to allow interoperability with errors.Is(), errors.As() and errors.Unwrap()
|
||||
func (msg *Error) Unwrap() error {
|
||||
return msg.Err
|
||||
}
|
||||
|
||||
// ByType returns a readonly copy filtered the byte.
|
||||
// ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic.
|
||||
func (a errorMsgs) ByType(typ ErrorType) errorMsgs {
|
||||
if len(a) == 0 {
|
||||
return nil
|
||||
}
|
||||
if typ == ErrorTypeAny {
|
||||
return a
|
||||
}
|
||||
var result errorMsgs
|
||||
for _, msg := range a {
|
||||
if msg.IsType(typ) {
|
||||
result = append(result, msg)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Last returns the last error in the slice. It returns nil if the array is empty.
|
||||
// Shortcut for errors[len(errors)-1].
|
||||
func (a errorMsgs) Last() *Error {
|
||||
if length := len(a); length > 0 {
|
||||
return a[length-1]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Errors returns an array with all the error messages.
|
||||
// Example:
|
||||
//
|
||||
// c.Error(errors.New("first"))
|
||||
// c.Error(errors.New("second"))
|
||||
// c.Error(errors.New("third"))
|
||||
// c.Errors.Errors() // == []string{"first", "second", "third"}
|
||||
func (a errorMsgs) Errors() []string {
|
||||
if len(a) == 0 {
|
||||
return nil
|
||||
}
|
||||
errorStrings := make([]string, len(a))
|
||||
for i, err := range a {
|
||||
errorStrings[i] = err.Error()
|
||||
}
|
||||
return errorStrings
|
||||
}
|
||||
|
||||
func (a errorMsgs) JSON() any {
|
||||
switch length := len(a); length {
|
||||
case 0:
|
||||
return nil
|
||||
case 1:
|
||||
return a.Last().JSON()
|
||||
default:
|
||||
jsonData := make([]any, length)
|
||||
for i, err := range a {
|
||||
jsonData[i] = err.JSON()
|
||||
}
|
||||
return jsonData
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalJSON implements the json.Marshaller interface.
|
||||
func (a errorMsgs) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(a.JSON())
|
||||
}
|
||||
|
||||
func (a errorMsgs) String() string {
|
||||
if len(a) == 0 {
|
||||
return ""
|
||||
}
|
||||
var buffer strings.Builder
|
||||
for i, msg := range a {
|
||||
fmt.Fprintf(&buffer, "Error #%02d: %s\n", i+1, msg.Err)
|
||||
if msg.Meta != nil {
|
||||
fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)
|
||||
}
|
||||
}
|
||||
return buffer.String()
|
||||
}
|
128
errors_test.go
128
errors_test.go
|
@ -1,128 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"git.internal/re/gin/internal/json"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestError(t *testing.T) {
|
||||
baseError := errors.New("test error")
|
||||
err := &Error{
|
||||
Err: baseError,
|
||||
Type: ErrorTypePrivate,
|
||||
}
|
||||
assert.Equal(t, err.Error(), baseError.Error())
|
||||
assert.Equal(t, H{"error": baseError.Error()}, err.JSON())
|
||||
|
||||
assert.Equal(t, err.SetType(ErrorTypePublic), err)
|
||||
assert.Equal(t, ErrorTypePublic, err.Type)
|
||||
|
||||
assert.Equal(t, err.SetMeta("some data"), err)
|
||||
assert.Equal(t, "some data", err.Meta)
|
||||
assert.Equal(t, H{
|
||||
"error": baseError.Error(),
|
||||
"meta": "some data",
|
||||
}, err.JSON())
|
||||
|
||||
jsonBytes, _ := json.Marshal(err)
|
||||
assert.Equal(t, "{\"error\":\"test error\",\"meta\":\"some data\"}", string(jsonBytes))
|
||||
|
||||
err.SetMeta(H{ //nolint: errcheck
|
||||
"status": "200",
|
||||
"data": "some data",
|
||||
})
|
||||
assert.Equal(t, H{
|
||||
"error": baseError.Error(),
|
||||
"status": "200",
|
||||
"data": "some data",
|
||||
}, err.JSON())
|
||||
|
||||
err.SetMeta(H{ //nolint: errcheck
|
||||
"error": "custom error",
|
||||
"status": "200",
|
||||
"data": "some data",
|
||||
})
|
||||
assert.Equal(t, H{
|
||||
"error": "custom error",
|
||||
"status": "200",
|
||||
"data": "some data",
|
||||
}, err.JSON())
|
||||
|
||||
type customError struct {
|
||||
status string
|
||||
data string
|
||||
}
|
||||
err.SetMeta(customError{status: "200", data: "other data"}) //nolint: errcheck
|
||||
assert.Equal(t, customError{status: "200", data: "other data"}, err.JSON())
|
||||
}
|
||||
|
||||
func TestErrorSlice(t *testing.T) {
|
||||
errs := errorMsgs{
|
||||
{Err: errors.New("first"), Type: ErrorTypePrivate},
|
||||
{Err: errors.New("second"), Type: ErrorTypePrivate, Meta: "some data"},
|
||||
{Err: errors.New("third"), Type: ErrorTypePublic, Meta: H{"status": "400"}},
|
||||
}
|
||||
|
||||
assert.Equal(t, errs, errs.ByType(ErrorTypeAny))
|
||||
assert.Equal(t, "third", errs.Last().Error())
|
||||
assert.Equal(t, []string{"first", "second", "third"}, errs.Errors())
|
||||
assert.Equal(t, []string{"third"}, errs.ByType(ErrorTypePublic).Errors())
|
||||
assert.Equal(t, []string{"first", "second"}, errs.ByType(ErrorTypePrivate).Errors())
|
||||
assert.Equal(t, []string{"first", "second", "third"}, errs.ByType(ErrorTypePublic|ErrorTypePrivate).Errors())
|
||||
assert.Empty(t, errs.ByType(ErrorTypeBind))
|
||||
assert.Empty(t, errs.ByType(ErrorTypeBind).String())
|
||||
|
||||
assert.Equal(t, `Error #01: first
|
||||
Error #02: second
|
||||
Meta: some data
|
||||
Error #03: third
|
||||
Meta: map[status:400]
|
||||
`, errs.String())
|
||||
assert.Equal(t, []any{
|
||||
H{"error": "first"},
|
||||
H{"error": "second", "meta": "some data"},
|
||||
H{"error": "third", "status": "400"},
|
||||
}, errs.JSON())
|
||||
jsonBytes, _ := json.Marshal(errs)
|
||||
assert.Equal(t, "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]", string(jsonBytes))
|
||||
errs = errorMsgs{
|
||||
{Err: errors.New("first"), Type: ErrorTypePrivate},
|
||||
}
|
||||
assert.Equal(t, H{"error": "first"}, errs.JSON())
|
||||
jsonBytes, _ = json.Marshal(errs)
|
||||
assert.Equal(t, "{\"error\":\"first\"}", string(jsonBytes))
|
||||
|
||||
errs = errorMsgs{}
|
||||
assert.Nil(t, errs.Last())
|
||||
assert.Nil(t, errs.JSON())
|
||||
assert.Empty(t, errs.String())
|
||||
}
|
||||
|
||||
type TestErr string
|
||||
|
||||
func (e TestErr) Error() string { return string(e) }
|
||||
|
||||
// TestErrorUnwrap tests the behavior of gin.Error with "errors.Is()" and "errors.As()".
|
||||
// "errors.Is()" and "errors.As()" have been added to the standard library in go 1.13.
|
||||
func TestErrorUnwrap(t *testing.T) {
|
||||
innerErr := TestErr("some error")
|
||||
|
||||
// 2 layers of wrapping : use 'fmt.Errorf("%w")' to wrap a gin.Error{}, which itself wraps innerErr
|
||||
err := fmt.Errorf("wrapped: %w", &Error{
|
||||
Err: innerErr,
|
||||
Type: ErrorTypeAny,
|
||||
})
|
||||
|
||||
// check that 'errors.Is()' and 'errors.As()' behave as expected :
|
||||
assert.True(t, errors.Is(err, innerErr))
|
||||
var testErr TestErr
|
||||
assert.True(t, errors.As(err, &testErr))
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
# Gin Examples
|
||||
|
||||
⚠️ **NOTICE:** All gin examples have been moved as standalone repository to [here](https://github.com/gin-gonic/examples).
|
|
@ -0,0 +1,7 @@
|
|||
# Guide to run Gin under App Engine LOCAL Development Server
|
||||
|
||||
1. Download, install and setup Go in your computer. (That includes setting your `$GOPATH`.)
|
||||
2. Download SDK for your platform from here: `https://developers.google.com/appengine/downloads?hl=es#Google_App_Engine_SDK_for_Go`
|
||||
3. Download Gin source code using: `$ go get github.com/gin-gonic/gin`
|
||||
4. Navigate to examples folder: `$ cd $GOPATH/src/github.com/gin-gonic/gin/examples/`
|
||||
5. Run it: `$ goapp serve app-engine/`
|
|
@ -0,0 +1,8 @@
|
|||
application: hello
|
||||
version: 1
|
||||
runtime: go
|
||||
api_version: go1
|
||||
|
||||
handlers:
|
||||
- url: /.*
|
||||
script: _go_app
|
|
@ -0,0 +1,23 @@
|
|||
package hello
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// This function's name is a must. App Engine uses it to drive the requests properly.
|
||||
func init() {
|
||||
// Starts a new Gin instance with no middle-ware
|
||||
r := gin.New()
|
||||
|
||||
// Define your handlers
|
||||
r.GET("/", func(c *gin.Context){
|
||||
c.String(200, "Hello World!")
|
||||
})
|
||||
r.GET("/ping", func(c *gin.Context){
|
||||
c.String(200, "pong")
|
||||
})
|
||||
|
||||
// Handle all requests using net/http
|
||||
http.Handle("/", r)
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var DB = make(map[string]string)
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
|
||||
// Ping test
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.String(200, "pong")
|
||||
})
|
||||
|
||||
// Get user value
|
||||
r.GET("/user/:name", func(c *gin.Context) {
|
||||
user := c.Params.ByName("name")
|
||||
value, ok := DB[user]
|
||||
if ok {
|
||||
c.JSON(200, gin.H{"user": user, "value": value})
|
||||
} else {
|
||||
c.JSON(200, gin.H{"user": user, "status": "no value"})
|
||||
}
|
||||
})
|
||||
|
||||
// Authorized group (uses gin.BasicAuth() middleware)
|
||||
// Same than:
|
||||
// authorized := r.Group("/")
|
||||
// authorized.Use(gin.BasicAuth(gin.Credentials{
|
||||
// "foo": "bar",
|
||||
// "manu": "123",
|
||||
//}))
|
||||
authorized := r.Group("/", gin.BasicAuth(gin.Accounts{
|
||||
"foo": "bar", // user:foo password:bar
|
||||
"manu": "123", // user:manu password:123
|
||||
}))
|
||||
|
||||
authorized.POST("admin", func(c *gin.Context) {
|
||||
user := c.MustGet(gin.AuthUserKey).(string)
|
||||
|
||||
// Parse JSON
|
||||
var json struct {
|
||||
Value string `json:"value" binding:"required"`
|
||||
}
|
||||
if c.EnsureBody(&json) {
|
||||
DB[user] = json.Value
|
||||
c.JSON(200, gin.H{"status": "ok"})
|
||||
}
|
||||
})
|
||||
|
||||
// Listen and Server in 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
}
|
45
fs.go
45
fs.go
|
@ -1,45 +0,0 @@
|
|||
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type onlyFilesFS struct {
|
||||
fs http.FileSystem
|
||||
}
|
||||
|
||||
type neuteredReaddirFile struct {
|
||||
http.File
|
||||
}
|
||||
|
||||
// Dir returns a http.FileSystem that can be used by http.FileServer(). It is used internally
|
||||
// in router.Static().
|
||||
// if listDirectory == true, then it works the same as http.Dir() otherwise it returns
|
||||
// a filesystem that prevents http.FileServer() to list the directory files.
|
||||
func Dir(root string, listDirectory bool) http.FileSystem {
|
||||
fs := http.Dir(root)
|
||||
if listDirectory {
|
||||
return fs
|
||||
}
|
||||
return &onlyFilesFS{fs}
|
||||
}
|
||||
|
||||
// Open conforms to http.Filesystem.
|
||||
func (fs onlyFilesFS) Open(name string) (http.File, error) {
|
||||
f, err := fs.fs.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return neuteredReaddirFile{f}, nil
|
||||
}
|
||||
|
||||
// Readdir overrides the http.File default implementation.
|
||||
func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) {
|
||||
// this disables directory listing
|
||||
return nil, nil
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
# Gin Default Server
|
||||
|
||||
This is API experiment for Gin.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"git.internal/re/gin"
|
||||
"git.internal/re/gin/ginS"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ginS.GET("/", func(c *gin.Context) { c.String(200, "Hello World") })
|
||||
ginS.Run()
|
||||
}
|
||||
```
|
162
ginS/gins.go
162
ginS/gins.go
|
@ -1,162 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package ginS
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"git.internal/re/gin"
|
||||
)
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
internalEngine *gin.Engine
|
||||
)
|
||||
|
||||
func engine() *gin.Engine {
|
||||
once.Do(func() {
|
||||
internalEngine = gin.Default()
|
||||
})
|
||||
return internalEngine
|
||||
}
|
||||
|
||||
// LoadHTMLGlob is a wrapper for Engine.LoadHTMLGlob.
|
||||
func LoadHTMLGlob(pattern string) {
|
||||
engine().LoadHTMLGlob(pattern)
|
||||
}
|
||||
|
||||
// LoadHTMLFiles is a wrapper for Engine.LoadHTMLFiles.
|
||||
func LoadHTMLFiles(files ...string) {
|
||||
engine().LoadHTMLFiles(files...)
|
||||
}
|
||||
|
||||
// SetHTMLTemplate is a wrapper for Engine.SetHTMLTemplate.
|
||||
func SetHTMLTemplate(templ *template.Template) {
|
||||
engine().SetHTMLTemplate(templ)
|
||||
}
|
||||
|
||||
// NoRoute adds handlers for NoRoute. It returns a 404 code by default.
|
||||
func NoRoute(handlers ...gin.HandlerFunc) {
|
||||
engine().NoRoute(handlers...)
|
||||
}
|
||||
|
||||
// NoMethod is a wrapper for Engine.NoMethod.
|
||||
func NoMethod(handlers ...gin.HandlerFunc) {
|
||||
engine().NoMethod(handlers...)
|
||||
}
|
||||
|
||||
// Group creates a new router group. You should add all the routes that have common middlewares or the same path prefix.
|
||||
// For example, all the routes that use a common middleware for authorization could be grouped.
|
||||
func Group(relativePath string, handlers ...gin.HandlerFunc) *gin.RouterGroup {
|
||||
return engine().Group(relativePath, handlers...)
|
||||
}
|
||||
|
||||
// Handle is a wrapper for Engine.Handle.
|
||||
func Handle(httpMethod, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return engine().Handle(httpMethod, relativePath, handlers...)
|
||||
}
|
||||
|
||||
// POST is a shortcut for router.Handle("POST", path, handle)
|
||||
func POST(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return engine().POST(relativePath, handlers...)
|
||||
}
|
||||
|
||||
// GET is a shortcut for router.Handle("GET", path, handle)
|
||||
func GET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return engine().GET(relativePath, handlers...)
|
||||
}
|
||||
|
||||
// DELETE is a shortcut for router.Handle("DELETE", path, handle)
|
||||
func DELETE(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return engine().DELETE(relativePath, handlers...)
|
||||
}
|
||||
|
||||
// PATCH is a shortcut for router.Handle("PATCH", path, handle)
|
||||
func PATCH(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return engine().PATCH(relativePath, handlers...)
|
||||
}
|
||||
|
||||
// PUT is a shortcut for router.Handle("PUT", path, handle)
|
||||
func PUT(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return engine().PUT(relativePath, handlers...)
|
||||
}
|
||||
|
||||
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
|
||||
func OPTIONS(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return engine().OPTIONS(relativePath, handlers...)
|
||||
}
|
||||
|
||||
// HEAD is a shortcut for router.Handle("HEAD", path, handle)
|
||||
func HEAD(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return engine().HEAD(relativePath, handlers...)
|
||||
}
|
||||
|
||||
// Any is a wrapper for Engine.Any.
|
||||
func Any(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return engine().Any(relativePath, handlers...)
|
||||
}
|
||||
|
||||
// StaticFile is a wrapper for Engine.StaticFile.
|
||||
func StaticFile(relativePath, filepath string) gin.IRoutes {
|
||||
return engine().StaticFile(relativePath, filepath)
|
||||
}
|
||||
|
||||
// Static serves files from the given file system root.
|
||||
// Internally a http.FileServer is used, therefore http.NotFound is used instead
|
||||
// of the Router's NotFound handler.
|
||||
// To use the operating system's file system implementation,
|
||||
// use :
|
||||
//
|
||||
// router.Static("/static", "/var/www")
|
||||
func Static(relativePath, root string) gin.IRoutes {
|
||||
return engine().Static(relativePath, root)
|
||||
}
|
||||
|
||||
// StaticFS is a wrapper for Engine.StaticFS.
|
||||
func StaticFS(relativePath string, fs http.FileSystem) gin.IRoutes {
|
||||
return engine().StaticFS(relativePath, fs)
|
||||
}
|
||||
|
||||
// Use attaches a global middleware to the router. i.e. the middlewares attached through Use() will be
|
||||
// included in the handlers chain for every single request. Even 404, 405, static files...
|
||||
// For example, this is the right place for a logger or error management middleware.
|
||||
func Use(middlewares ...gin.HandlerFunc) gin.IRoutes {
|
||||
return engine().Use(middlewares...)
|
||||
}
|
||||
|
||||
// Routes returns a slice of registered routes.
|
||||
func Routes() gin.RoutesInfo {
|
||||
return engine().Routes()
|
||||
}
|
||||
|
||||
// Run attaches to a http.Server and starts listening and serving HTTP requests.
|
||||
// It is a shortcut for http.ListenAndServe(addr, router)
|
||||
// Note: this method will block the calling goroutine indefinitely unless an error happens.
|
||||
func Run(addr ...string) (err error) {
|
||||
return engine().Run(addr...)
|
||||
}
|
||||
|
||||
// RunTLS attaches to a http.Server and starts listening and serving HTTPS requests.
|
||||
// It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
|
||||
// Note: this method will block the calling goroutine indefinitely unless an error happens.
|
||||
func RunTLS(addr, certFile, keyFile string) (err error) {
|
||||
return engine().RunTLS(addr, certFile, keyFile)
|
||||
}
|
||||
|
||||
// RunUnix attaches to a http.Server and starts listening and serving HTTP requests
|
||||
// through the specified unix socket (i.e. a file)
|
||||
// Note: this method will block the calling goroutine indefinitely unless an error happens.
|
||||
func RunUnix(file string) (err error) {
|
||||
return engine().RunUnix(file)
|
||||
}
|
||||
|
||||
// RunFd attaches the router to a http.Server and starts listening and serving HTTP requests
|
||||
// through the specified file descriptor.
|
||||
// Note: the method will block the calling goroutine indefinitely unless on error happens.
|
||||
func RunFd(fd int) (err error) {
|
||||
return engine().RunFd(fd)
|
||||
}
|
|
@ -1,563 +0,0 @@
|
|||
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// params[0]=url example:http://127.0.0.1:8080/index (cannot be empty)
|
||||
// params[1]=response status (custom compare status) default:"200 OK"
|
||||
// params[2]=response body (custom compare content) default:"it worked"
|
||||
func testRequest(t *testing.T, params ...string) {
|
||||
|
||||
if len(params) == 0 {
|
||||
t.Fatal("url cannot be empty")
|
||||
}
|
||||
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
}
|
||||
client := &http.Client{Transport: tr}
|
||||
|
||||
resp, err := client.Get(params[0])
|
||||
assert.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, ioerr := io.ReadAll(resp.Body)
|
||||
assert.NoError(t, ioerr)
|
||||
|
||||
var responseStatus = "200 OK"
|
||||
if len(params) > 1 && params[1] != "" {
|
||||
responseStatus = params[1]
|
||||
}
|
||||
|
||||
var responseBody = "it worked"
|
||||
if len(params) > 2 && params[2] != "" {
|
||||
responseBody = params[2]
|
||||
}
|
||||
|
||||
assert.Equal(t, responseStatus, resp.Status, "should get a "+responseStatus)
|
||||
if responseStatus == "200 OK" {
|
||||
assert.Equal(t, responseBody, string(body), "resp body should match")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunEmpty(t *testing.T) {
|
||||
os.Setenv("PORT", "")
|
||||
router := New()
|
||||
go func() {
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
assert.NoError(t, router.Run())
|
||||
}()
|
||||
// have to wait for the goroutine to start and run the server
|
||||
// otherwise the main thread will complete
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
|
||||
assert.Error(t, router.Run(":8080"))
|
||||
testRequest(t, "http://localhost:8080/example")
|
||||
}
|
||||
|
||||
func TestBadTrustedCIDRs(t *testing.T) {
|
||||
router := New()
|
||||
assert.Error(t, router.SetTrustedProxies([]string{"hello/world"}))
|
||||
}
|
||||
|
||||
/* legacy tests
|
||||
func TestBadTrustedCIDRsForRun(t *testing.T) {
|
||||
os.Setenv("PORT", "")
|
||||
router := New()
|
||||
router.TrustedProxies = []string{"hello/world"}
|
||||
assert.Error(t, router.Run(":8080"))
|
||||
}
|
||||
|
||||
func TestBadTrustedCIDRsForRunUnix(t *testing.T) {
|
||||
router := New()
|
||||
router.TrustedProxies = []string{"hello/world"}
|
||||
|
||||
unixTestSocket := filepath.Join(os.TempDir(), "unix_unit_test")
|
||||
|
||||
defer os.Remove(unixTestSocket)
|
||||
|
||||
go func() {
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
assert.Error(t, router.RunUnix(unixTestSocket))
|
||||
}()
|
||||
// have to wait for the goroutine to start and run the server
|
||||
// otherwise the main thread will complete
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
|
||||
func TestBadTrustedCIDRsForRunFd(t *testing.T) {
|
||||
router := New()
|
||||
router.TrustedProxies = []string{"hello/world"}
|
||||
|
||||
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||||
assert.NoError(t, err)
|
||||
listener, err := net.ListenTCP("tcp", addr)
|
||||
assert.NoError(t, err)
|
||||
socketFile, err := listener.File()
|
||||
assert.NoError(t, err)
|
||||
|
||||
go func() {
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
assert.Error(t, router.RunFd(int(socketFile.Fd())))
|
||||
}()
|
||||
// have to wait for the goroutine to start and run the server
|
||||
// otherwise the main thread will complete
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
|
||||
func TestBadTrustedCIDRsForRunListener(t *testing.T) {
|
||||
router := New()
|
||||
router.TrustedProxies = []string{"hello/world"}
|
||||
|
||||
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||||
assert.NoError(t, err)
|
||||
listener, err := net.ListenTCP("tcp", addr)
|
||||
assert.NoError(t, err)
|
||||
go func() {
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
assert.Error(t, router.RunListener(listener))
|
||||
}()
|
||||
// have to wait for the goroutine to start and run the server
|
||||
// otherwise the main thread will complete
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
|
||||
func TestBadTrustedCIDRsForRunTLS(t *testing.T) {
|
||||
os.Setenv("PORT", "")
|
||||
router := New()
|
||||
router.TrustedProxies = []string{"hello/world"}
|
||||
assert.Error(t, router.RunTLS(":8080", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"))
|
||||
}
|
||||
*/
|
||||
|
||||
func TestRunTLS(t *testing.T) {
|
||||
router := New()
|
||||
go func() {
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
|
||||
assert.NoError(t, router.RunTLS(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"))
|
||||
}()
|
||||
|
||||
// have to wait for the goroutine to start and run the server
|
||||
// otherwise the main thread will complete
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
|
||||
assert.Error(t, router.RunTLS(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"))
|
||||
testRequest(t, "https://localhost:8443/example")
|
||||
}
|
||||
|
||||
func TestPusher(t *testing.T) {
|
||||
var html = template.Must(template.New("https").Parse(`
|
||||
<html>
|
||||
<head>
|
||||
<title>Https Test</title>
|
||||
<script src="/assets/app.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1 style="color:red;">Welcome, Ginner!</h1>
|
||||
</body>
|
||||
</html>
|
||||
`))
|
||||
|
||||
router := New()
|
||||
router.Static("./assets", "./assets")
|
||||
router.SetHTMLTemplate(html)
|
||||
|
||||
go func() {
|
||||
router.GET("/pusher", func(c *Context) {
|
||||
if pusher := c.Writer.Pusher(); pusher != nil {
|
||||
err := pusher.Push("/assets/app.js", nil)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
c.String(http.StatusOK, "it worked")
|
||||
})
|
||||
|
||||
assert.NoError(t, router.RunTLS(":8449", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"))
|
||||
}()
|
||||
|
||||
// have to wait for the goroutine to start and run the server
|
||||
// otherwise the main thread will complete
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
|
||||
assert.Error(t, router.RunTLS(":8449", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"))
|
||||
testRequest(t, "https://localhost:8449/pusher")
|
||||
}
|
||||
|
||||
func TestRunEmptyWithEnv(t *testing.T) {
|
||||
os.Setenv("PORT", "3123")
|
||||
router := New()
|
||||
go func() {
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
assert.NoError(t, router.Run())
|
||||
}()
|
||||
// have to wait for the goroutine to start and run the server
|
||||
// otherwise the main thread will complete
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
|
||||
assert.Error(t, router.Run(":3123"))
|
||||
testRequest(t, "http://localhost:3123/example")
|
||||
}
|
||||
|
||||
func TestRunTooMuchParams(t *testing.T) {
|
||||
router := New()
|
||||
assert.Panics(t, func() {
|
||||
assert.NoError(t, router.Run("2", "2"))
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunWithPort(t *testing.T) {
|
||||
router := New()
|
||||
go func() {
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
assert.NoError(t, router.Run(":5150"))
|
||||
}()
|
||||
// have to wait for the goroutine to start and run the server
|
||||
// otherwise the main thread will complete
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
|
||||
assert.Error(t, router.Run(":5150"))
|
||||
testRequest(t, "http://localhost:5150/example")
|
||||
}
|
||||
|
||||
func TestUnixSocket(t *testing.T) {
|
||||
router := New()
|
||||
|
||||
unixTestSocket := filepath.Join(os.TempDir(), "unix_unit_test")
|
||||
|
||||
defer os.Remove(unixTestSocket)
|
||||
|
||||
go func() {
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
assert.NoError(t, router.RunUnix(unixTestSocket))
|
||||
}()
|
||||
// have to wait for the goroutine to start and run the server
|
||||
// otherwise the main thread will complete
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
|
||||
c, err := net.Dial("unix", unixTestSocket)
|
||||
assert.NoError(t, err)
|
||||
|
||||
fmt.Fprint(c, "GET /example HTTP/1.0\r\n\r\n")
|
||||
scanner := bufio.NewScanner(c)
|
||||
var response string
|
||||
for scanner.Scan() {
|
||||
response += scanner.Text()
|
||||
}
|
||||
assert.Contains(t, response, "HTTP/1.0 200", "should get a 200")
|
||||
assert.Contains(t, response, "it worked", "resp body should match")
|
||||
}
|
||||
|
||||
func TestBadUnixSocket(t *testing.T) {
|
||||
router := New()
|
||||
assert.Error(t, router.RunUnix("#/tmp/unix_unit_test"))
|
||||
}
|
||||
|
||||
func TestFileDescriptor(t *testing.T) {
|
||||
router := New()
|
||||
|
||||
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||||
assert.NoError(t, err)
|
||||
listener, err := net.ListenTCP("tcp", addr)
|
||||
assert.NoError(t, err)
|
||||
socketFile, err := listener.File()
|
||||
if isWindows() {
|
||||
// not supported by windows, it is unimplemented now
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
if socketFile == nil {
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
assert.NoError(t, router.RunFd(int(socketFile.Fd())))
|
||||
}()
|
||||
// have to wait for the goroutine to start and run the server
|
||||
// otherwise the main thread will complete
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
|
||||
c, err := net.Dial("tcp", listener.Addr().String())
|
||||
assert.NoError(t, err)
|
||||
|
||||
fmt.Fprintf(c, "GET /example HTTP/1.0\r\n\r\n")
|
||||
scanner := bufio.NewScanner(c)
|
||||
var response string
|
||||
for scanner.Scan() {
|
||||
response += scanner.Text()
|
||||
}
|
||||
assert.Contains(t, response, "HTTP/1.0 200", "should get a 200")
|
||||
assert.Contains(t, response, "it worked", "resp body should match")
|
||||
}
|
||||
|
||||
func TestBadFileDescriptor(t *testing.T) {
|
||||
router := New()
|
||||
assert.Error(t, router.RunFd(0))
|
||||
}
|
||||
|
||||
func TestListener(t *testing.T) {
|
||||
router := New()
|
||||
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||||
assert.NoError(t, err)
|
||||
listener, err := net.ListenTCP("tcp", addr)
|
||||
assert.NoError(t, err)
|
||||
go func() {
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
assert.NoError(t, router.RunListener(listener))
|
||||
}()
|
||||
// have to wait for the goroutine to start and run the server
|
||||
// otherwise the main thread will complete
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
|
||||
c, err := net.Dial("tcp", listener.Addr().String())
|
||||
assert.NoError(t, err)
|
||||
|
||||
fmt.Fprintf(c, "GET /example HTTP/1.0\r\n\r\n")
|
||||
scanner := bufio.NewScanner(c)
|
||||
var response string
|
||||
for scanner.Scan() {
|
||||
response += scanner.Text()
|
||||
}
|
||||
assert.Contains(t, response, "HTTP/1.0 200", "should get a 200")
|
||||
assert.Contains(t, response, "it worked", "resp body should match")
|
||||
}
|
||||
|
||||
func TestBadListener(t *testing.T) {
|
||||
router := New()
|
||||
addr, err := net.ResolveTCPAddr("tcp", "localhost:10086")
|
||||
assert.NoError(t, err)
|
||||
listener, err := net.ListenTCP("tcp", addr)
|
||||
assert.NoError(t, err)
|
||||
listener.Close()
|
||||
assert.Error(t, router.RunListener(listener))
|
||||
}
|
||||
|
||||
func TestWithHttptestWithAutoSelectedPort(t *testing.T) {
|
||||
router := New()
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
|
||||
ts := httptest.NewServer(router)
|
||||
defer ts.Close()
|
||||
|
||||
testRequest(t, ts.URL+"/example")
|
||||
}
|
||||
|
||||
func TestConcurrentHandleContext(t *testing.T) {
|
||||
router := New()
|
||||
router.GET("/", func(c *Context) {
|
||||
c.Request.URL.Path = "/example"
|
||||
router.HandleContext(c)
|
||||
})
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
|
||||
var wg sync.WaitGroup
|
||||
iterations := 200
|
||||
wg.Add(iterations)
|
||||
for i := 0; i < iterations; i++ {
|
||||
go func() {
|
||||
testGetRequestHandler(t, router, "/")
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// func TestWithHttptestWithSpecifiedPort(t *testing.T) {
|
||||
// router := New()
|
||||
// router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
|
||||
// l, _ := net.Listen("tcp", ":8033")
|
||||
// ts := httptest.Server{
|
||||
// Listener: l,
|
||||
// Config: &http.Server{Handler: router},
|
||||
// }
|
||||
// ts.Start()
|
||||
// defer ts.Close()
|
||||
|
||||
// testRequest(t, "http://localhost:8033/example")
|
||||
// }
|
||||
|
||||
func testGetRequestHandler(t *testing.T, h http.Handler, url string) {
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, "it worked", w.Body.String(), "resp body should match")
|
||||
assert.Equal(t, 200, w.Code, "should get a 200")
|
||||
}
|
||||
|
||||
func TestTreeRunDynamicRouting(t *testing.T) {
|
||||
router := New()
|
||||
router.GET("/aa/*xx", func(c *Context) { c.String(http.StatusOK, "/aa/*xx") })
|
||||
router.GET("/ab/*xx", func(c *Context) { c.String(http.StatusOK, "/ab/*xx") })
|
||||
router.GET("/", func(c *Context) { c.String(http.StatusOK, "home") })
|
||||
router.GET("/:cc", func(c *Context) { c.String(http.StatusOK, "/:cc") })
|
||||
router.GET("/c1/:dd/e", func(c *Context) { c.String(http.StatusOK, "/c1/:dd/e") })
|
||||
router.GET("/c1/:dd/e1", func(c *Context) { c.String(http.StatusOK, "/c1/:dd/e1") })
|
||||
router.GET("/c1/:dd/f1", func(c *Context) { c.String(http.StatusOK, "/c1/:dd/f1") })
|
||||
router.GET("/c1/:dd/f2", func(c *Context) { c.String(http.StatusOK, "/c1/:dd/f2") })
|
||||
router.GET("/:cc/cc", func(c *Context) { c.String(http.StatusOK, "/:cc/cc") })
|
||||
router.GET("/:cc/:dd/ee", func(c *Context) { c.String(http.StatusOK, "/:cc/:dd/ee") })
|
||||
router.GET("/:cc/:dd/f", func(c *Context) { c.String(http.StatusOK, "/:cc/:dd/f") })
|
||||
router.GET("/:cc/:dd/:ee/ff", func(c *Context) { c.String(http.StatusOK, "/:cc/:dd/:ee/ff") })
|
||||
router.GET("/:cc/:dd/:ee/:ff/gg", func(c *Context) { c.String(http.StatusOK, "/:cc/:dd/:ee/:ff/gg") })
|
||||
router.GET("/:cc/:dd/:ee/:ff/:gg/hh", func(c *Context) { c.String(http.StatusOK, "/:cc/:dd/:ee/:ff/:gg/hh") })
|
||||
router.GET("/get/test/abc/", func(c *Context) { c.String(http.StatusOK, "/get/test/abc/") })
|
||||
router.GET("/get/:param/abc/", func(c *Context) { c.String(http.StatusOK, "/get/:param/abc/") })
|
||||
router.GET("/something/:paramname/thirdthing", func(c *Context) { c.String(http.StatusOK, "/something/:paramname/thirdthing") })
|
||||
router.GET("/something/secondthing/test", func(c *Context) { c.String(http.StatusOK, "/something/secondthing/test") })
|
||||
router.GET("/get/abc", func(c *Context) { c.String(http.StatusOK, "/get/abc") })
|
||||
router.GET("/get/:param", func(c *Context) { c.String(http.StatusOK, "/get/:param") })
|
||||
router.GET("/get/abc/123abc", func(c *Context) { c.String(http.StatusOK, "/get/abc/123abc") })
|
||||
router.GET("/get/abc/:param", func(c *Context) { c.String(http.StatusOK, "/get/abc/:param") })
|
||||
router.GET("/get/abc/123abc/xxx8", func(c *Context) { c.String(http.StatusOK, "/get/abc/123abc/xxx8") })
|
||||
router.GET("/get/abc/123abc/:param", func(c *Context) { c.String(http.StatusOK, "/get/abc/123abc/:param") })
|
||||
router.GET("/get/abc/123abc/xxx8/1234", func(c *Context) { c.String(http.StatusOK, "/get/abc/123abc/xxx8/1234") })
|
||||
router.GET("/get/abc/123abc/xxx8/:param", func(c *Context) { c.String(http.StatusOK, "/get/abc/123abc/xxx8/:param") })
|
||||
router.GET("/get/abc/123abc/xxx8/1234/ffas", func(c *Context) { c.String(http.StatusOK, "/get/abc/123abc/xxx8/1234/ffas") })
|
||||
router.GET("/get/abc/123abc/xxx8/1234/:param", func(c *Context) { c.String(http.StatusOK, "/get/abc/123abc/xxx8/1234/:param") })
|
||||
router.GET("/get/abc/123abc/xxx8/1234/kkdd/12c", func(c *Context) { c.String(http.StatusOK, "/get/abc/123abc/xxx8/1234/kkdd/12c") })
|
||||
router.GET("/get/abc/123abc/xxx8/1234/kkdd/:param", func(c *Context) { c.String(http.StatusOK, "/get/abc/123abc/xxx8/1234/kkdd/:param") })
|
||||
router.GET("/get/abc/:param/test", func(c *Context) { c.String(http.StatusOK, "/get/abc/:param/test") })
|
||||
router.GET("/get/abc/123abd/:param", func(c *Context) { c.String(http.StatusOK, "/get/abc/123abd/:param") })
|
||||
router.GET("/get/abc/123abddd/:param", func(c *Context) { c.String(http.StatusOK, "/get/abc/123abddd/:param") })
|
||||
router.GET("/get/abc/123/:param", func(c *Context) { c.String(http.StatusOK, "/get/abc/123/:param") })
|
||||
router.GET("/get/abc/123abg/:param", func(c *Context) { c.String(http.StatusOK, "/get/abc/123abg/:param") })
|
||||
router.GET("/get/abc/123abf/:param", func(c *Context) { c.String(http.StatusOK, "/get/abc/123abf/:param") })
|
||||
router.GET("/get/abc/123abfff/:param", func(c *Context) { c.String(http.StatusOK, "/get/abc/123abfff/:param") })
|
||||
|
||||
ts := httptest.NewServer(router)
|
||||
defer ts.Close()
|
||||
|
||||
testRequest(t, ts.URL+"/", "", "home")
|
||||
testRequest(t, ts.URL+"/aa/aa", "", "/aa/*xx")
|
||||
testRequest(t, ts.URL+"/ab/ab", "", "/ab/*xx")
|
||||
testRequest(t, ts.URL+"/all", "", "/:cc")
|
||||
testRequest(t, ts.URL+"/all/cc", "", "/:cc/cc")
|
||||
testRequest(t, ts.URL+"/a/cc", "", "/:cc/cc")
|
||||
testRequest(t, ts.URL+"/c1/d/e", "", "/c1/:dd/e")
|
||||
testRequest(t, ts.URL+"/c1/d/e1", "", "/c1/:dd/e1")
|
||||
testRequest(t, ts.URL+"/c1/d/ee", "", "/:cc/:dd/ee")
|
||||
testRequest(t, ts.URL+"/c1/d/f", "", "/:cc/:dd/f")
|
||||
testRequest(t, ts.URL+"/c/d/ee", "", "/:cc/:dd/ee")
|
||||
testRequest(t, ts.URL+"/c/d/e/ff", "", "/:cc/:dd/:ee/ff")
|
||||
testRequest(t, ts.URL+"/c/d/e/f/gg", "", "/:cc/:dd/:ee/:ff/gg")
|
||||
testRequest(t, ts.URL+"/c/d/e/f/g/hh", "", "/:cc/:dd/:ee/:ff/:gg/hh")
|
||||
testRequest(t, ts.URL+"/cc/dd/ee/ff/gg/hh", "", "/:cc/:dd/:ee/:ff/:gg/hh")
|
||||
testRequest(t, ts.URL+"/a", "", "/:cc")
|
||||
testRequest(t, ts.URL+"/d", "", "/:cc")
|
||||
testRequest(t, ts.URL+"/ad", "", "/:cc")
|
||||
testRequest(t, ts.URL+"/dd", "", "/:cc")
|
||||
testRequest(t, ts.URL+"/aa", "", "/:cc")
|
||||
testRequest(t, ts.URL+"/aaa", "", "/:cc")
|
||||
testRequest(t, ts.URL+"/aaa/cc", "", "/:cc/cc")
|
||||
testRequest(t, ts.URL+"/ab", "", "/:cc")
|
||||
testRequest(t, ts.URL+"/abb", "", "/:cc")
|
||||
testRequest(t, ts.URL+"/abb/cc", "", "/:cc/cc")
|
||||
testRequest(t, ts.URL+"/dddaa", "", "/:cc")
|
||||
testRequest(t, ts.URL+"/allxxxx", "", "/:cc")
|
||||
testRequest(t, ts.URL+"/alldd", "", "/:cc")
|
||||
testRequest(t, ts.URL+"/cc/cc", "", "/:cc/cc")
|
||||
testRequest(t, ts.URL+"/ccc/cc", "", "/:cc/cc")
|
||||
testRequest(t, ts.URL+"/deedwjfs/cc", "", "/:cc/cc")
|
||||
testRequest(t, ts.URL+"/acllcc/cc", "", "/:cc/cc")
|
||||
testRequest(t, ts.URL+"/get/test/abc/", "", "/get/test/abc/")
|
||||
testRequest(t, ts.URL+"/get/testaa/abc/", "", "/get/:param/abc/")
|
||||
testRequest(t, ts.URL+"/get/te/abc/", "", "/get/:param/abc/")
|
||||
testRequest(t, ts.URL+"/get/xx/abc/", "", "/get/:param/abc/")
|
||||
testRequest(t, ts.URL+"/get/tt/abc/", "", "/get/:param/abc/")
|
||||
testRequest(t, ts.URL+"/get/a/abc/", "", "/get/:param/abc/")
|
||||
testRequest(t, ts.URL+"/get/t/abc/", "", "/get/:param/abc/")
|
||||
testRequest(t, ts.URL+"/get/aa/abc/", "", "/get/:param/abc/")
|
||||
testRequest(t, ts.URL+"/get/abas/abc/", "", "/get/:param/abc/")
|
||||
testRequest(t, ts.URL+"/something/secondthing/test", "", "/something/secondthing/test")
|
||||
testRequest(t, ts.URL+"/something/secondthingaaaa/thirdthing", "", "/something/:paramname/thirdthing")
|
||||
testRequest(t, ts.URL+"/something/abcdad/thirdthing", "", "/something/:paramname/thirdthing")
|
||||
testRequest(t, ts.URL+"/something/se/thirdthing", "", "/something/:paramname/thirdthing")
|
||||
testRequest(t, ts.URL+"/something/s/thirdthing", "", "/something/:paramname/thirdthing")
|
||||
testRequest(t, ts.URL+"/something/secondthing/thirdthing", "", "/something/:paramname/thirdthing")
|
||||
testRequest(t, ts.URL+"/get/abc", "", "/get/abc")
|
||||
testRequest(t, ts.URL+"/get/a", "", "/get/:param")
|
||||
testRequest(t, ts.URL+"/get/abz", "", "/get/:param")
|
||||
testRequest(t, ts.URL+"/get/12a", "", "/get/:param")
|
||||
testRequest(t, ts.URL+"/get/abcd", "", "/get/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc", "", "/get/abc/123abc")
|
||||
testRequest(t, ts.URL+"/get/abc/12", "", "/get/abc/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123ab", "", "/get/abc/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/xyz", "", "/get/abc/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abcddxx", "", "/get/abc/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8", "", "/get/abc/123abc/xxx8")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/x", "", "/get/abc/123abc/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx", "", "/get/abc/123abc/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/abc", "", "/get/abc/123abc/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8xxas", "", "/get/abc/123abc/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/1234", "", "/get/abc/123abc/xxx8/1234")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/1", "", "/get/abc/123abc/xxx8/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/123", "", "/get/abc/123abc/xxx8/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/78k", "", "/get/abc/123abc/xxx8/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/1234xxxd", "", "/get/abc/123abc/xxx8/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/1234/ffas", "", "/get/abc/123abc/xxx8/1234/ffas")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/1234/f", "", "/get/abc/123abc/xxx8/1234/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/1234/ffa", "", "/get/abc/123abc/xxx8/1234/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/1234/kka", "", "/get/abc/123abc/xxx8/1234/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/1234/ffas321", "", "/get/abc/123abc/xxx8/1234/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/1234/kkdd/12c", "", "/get/abc/123abc/xxx8/1234/kkdd/12c")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/1234/kkdd/1", "", "/get/abc/123abc/xxx8/1234/kkdd/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/1234/kkdd/12", "", "/get/abc/123abc/xxx8/1234/kkdd/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/1234/kkdd/12b", "", "/get/abc/123abc/xxx8/1234/kkdd/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/1234/kkdd/34", "", "/get/abc/123abc/xxx8/1234/kkdd/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abc/xxx8/1234/kkdd/12c2e3", "", "/get/abc/123abc/xxx8/1234/kkdd/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/12/test", "", "/get/abc/:param/test")
|
||||
testRequest(t, ts.URL+"/get/abc/123abdd/test", "", "/get/abc/:param/test")
|
||||
testRequest(t, ts.URL+"/get/abc/123abdddf/test", "", "/get/abc/:param/test")
|
||||
testRequest(t, ts.URL+"/get/abc/123ab/test", "", "/get/abc/:param/test")
|
||||
testRequest(t, ts.URL+"/get/abc/123abgg/test", "", "/get/abc/:param/test")
|
||||
testRequest(t, ts.URL+"/get/abc/123abff/test", "", "/get/abc/:param/test")
|
||||
testRequest(t, ts.URL+"/get/abc/123abffff/test", "", "/get/abc/:param/test")
|
||||
testRequest(t, ts.URL+"/get/abc/123abd/test", "", "/get/abc/123abd/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abddd/test", "", "/get/abc/123abddd/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123/test22", "", "/get/abc/123/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abg/test", "", "/get/abc/123abg/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abf/testss", "", "/get/abc/123abf/:param")
|
||||
testRequest(t, ts.URL+"/get/abc/123abfff/te", "", "/get/abc/123abfff/:param")
|
||||
// 404 not found
|
||||
testRequest(t, ts.URL+"/c/d/e", "404 Not Found")
|
||||
testRequest(t, ts.URL+"/c/d/e1", "404 Not Found")
|
||||
testRequest(t, ts.URL+"/c/d/eee", "404 Not Found")
|
||||
testRequest(t, ts.URL+"/c1/d/eee", "404 Not Found")
|
||||
testRequest(t, ts.URL+"/c1/d/e2", "404 Not Found")
|
||||
testRequest(t, ts.URL+"/cc/dd/ee/ff/gg/hh1", "404 Not Found")
|
||||
testRequest(t, ts.URL+"/a/dd", "404 Not Found")
|
||||
testRequest(t, ts.URL+"/addr/dd/aa", "404 Not Found")
|
||||
testRequest(t, ts.URL+"/something/secondthing/121", "404 Not Found")
|
||||
}
|
||||
|
||||
func isWindows() bool {
|
||||
return runtime.GOOS == "windows"
|
||||
}
|
698
gin_test.go
698
gin_test.go
|
@ -1,698 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"golang.org/x/net/http2"
|
||||
)
|
||||
|
||||
func formatAsDate(t time.Time) string {
|
||||
year, month, day := t.Date()
|
||||
return fmt.Sprintf("%d/%02d/%02d", year, month, day)
|
||||
}
|
||||
|
||||
func setupHTMLFiles(t *testing.T, mode string, tls bool, loadMethod func(*Engine)) *httptest.Server {
|
||||
SetMode(mode)
|
||||
defer SetMode(TestMode)
|
||||
|
||||
var router *Engine
|
||||
captureOutput(t, func() {
|
||||
router = New()
|
||||
router.Delims("{[{", "}]}")
|
||||
router.SetFuncMap(template.FuncMap{
|
||||
"formatAsDate": formatAsDate,
|
||||
})
|
||||
loadMethod(router)
|
||||
router.GET("/test", func(c *Context) {
|
||||
c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
|
||||
})
|
||||
router.GET("/raw", func(c *Context) {
|
||||
c.HTML(http.StatusOK, "raw.tmpl", map[string]any{
|
||||
"now": time.Date(2017, 0o7, 0o1, 0, 0, 0, 0, time.UTC),
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
var ts *httptest.Server
|
||||
|
||||
if tls {
|
||||
ts = httptest.NewTLSServer(router)
|
||||
} else {
|
||||
ts = httptest.NewServer(router)
|
||||
}
|
||||
|
||||
return ts
|
||||
}
|
||||
|
||||
func TestLoadHTMLGlobDebugMode(t *testing.T) {
|
||||
ts := setupHTMLFiles(
|
||||
t,
|
||||
DebugMode,
|
||||
false,
|
||||
func(router *Engine) {
|
||||
router.LoadHTMLGlob("./testdata/template/*")
|
||||
},
|
||||
)
|
||||
defer ts.Close()
|
||||
|
||||
res, err := http.Get(fmt.Sprintf("%s/test", ts.URL))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resp, _ := io.ReadAll(res.Body)
|
||||
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
||||
}
|
||||
|
||||
func TestH2c(t *testing.T) {
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
r := Default()
|
||||
r.UseH2C = true
|
||||
r.GET("/", func(c *Context) {
|
||||
c.String(200, "<h1>Hello world</h1>")
|
||||
})
|
||||
go func() {
|
||||
err := http.Serve(ln, r.Handler())
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
}()
|
||||
defer ln.Close()
|
||||
|
||||
url := "http://" + ln.Addr().String() + "/"
|
||||
|
||||
httpClient := http.Client{
|
||||
Transport: &http2.Transport{
|
||||
AllowHTTP: true,
|
||||
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
|
||||
return net.Dial(netw, addr)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
res, err := httpClient.Get(url)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resp, _ := io.ReadAll(res.Body)
|
||||
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
||||
}
|
||||
|
||||
func TestLoadHTMLGlobTestMode(t *testing.T) {
|
||||
ts := setupHTMLFiles(
|
||||
t,
|
||||
TestMode,
|
||||
false,
|
||||
func(router *Engine) {
|
||||
router.LoadHTMLGlob("./testdata/template/*")
|
||||
},
|
||||
)
|
||||
defer ts.Close()
|
||||
|
||||
res, err := http.Get(fmt.Sprintf("%s/test", ts.URL))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resp, _ := io.ReadAll(res.Body)
|
||||
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
||||
}
|
||||
|
||||
func TestLoadHTMLGlobReleaseMode(t *testing.T) {
|
||||
ts := setupHTMLFiles(
|
||||
t,
|
||||
ReleaseMode,
|
||||
false,
|
||||
func(router *Engine) {
|
||||
router.LoadHTMLGlob("./testdata/template/*")
|
||||
},
|
||||
)
|
||||
defer ts.Close()
|
||||
|
||||
res, err := http.Get(fmt.Sprintf("%s/test", ts.URL))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resp, _ := io.ReadAll(res.Body)
|
||||
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
||||
}
|
||||
|
||||
func TestLoadHTMLGlobUsingTLS(t *testing.T) {
|
||||
ts := setupHTMLFiles(
|
||||
t,
|
||||
DebugMode,
|
||||
true,
|
||||
func(router *Engine) {
|
||||
router.LoadHTMLGlob("./testdata/template/*")
|
||||
},
|
||||
)
|
||||
defer ts.Close()
|
||||
|
||||
// Use InsecureSkipVerify for avoiding `x509: certificate signed by unknown authority` error
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
}
|
||||
client := &http.Client{Transport: tr}
|
||||
res, err := client.Get(fmt.Sprintf("%s/test", ts.URL))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resp, _ := io.ReadAll(res.Body)
|
||||
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
||||
}
|
||||
|
||||
func TestLoadHTMLGlobFromFuncMap(t *testing.T) {
|
||||
ts := setupHTMLFiles(
|
||||
t,
|
||||
DebugMode,
|
||||
false,
|
||||
func(router *Engine) {
|
||||
router.LoadHTMLGlob("./testdata/template/*")
|
||||
},
|
||||
)
|
||||
defer ts.Close()
|
||||
|
||||
res, err := http.Get(fmt.Sprintf("%s/raw", ts.URL))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resp, _ := io.ReadAll(res.Body)
|
||||
assert.Equal(t, "Date: 2017/07/01", string(resp))
|
||||
}
|
||||
|
||||
func init() {
|
||||
SetMode(TestMode)
|
||||
}
|
||||
|
||||
func TestCreateEngine(t *testing.T) {
|
||||
router := New()
|
||||
assert.Equal(t, "/", router.basePath)
|
||||
assert.Equal(t, router.engine, router)
|
||||
assert.Empty(t, router.Handlers)
|
||||
}
|
||||
|
||||
func TestLoadHTMLFilesTestMode(t *testing.T) {
|
||||
ts := setupHTMLFiles(
|
||||
t,
|
||||
TestMode,
|
||||
false,
|
||||
func(router *Engine) {
|
||||
router.LoadHTMLFiles("./testdata/template/hello.tmpl", "./testdata/template/raw.tmpl")
|
||||
},
|
||||
)
|
||||
defer ts.Close()
|
||||
|
||||
res, err := http.Get(fmt.Sprintf("%s/test", ts.URL))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resp, _ := io.ReadAll(res.Body)
|
||||
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
||||
}
|
||||
|
||||
func TestLoadHTMLFilesDebugMode(t *testing.T) {
|
||||
ts := setupHTMLFiles(
|
||||
t,
|
||||
DebugMode,
|
||||
false,
|
||||
func(router *Engine) {
|
||||
router.LoadHTMLFiles("./testdata/template/hello.tmpl", "./testdata/template/raw.tmpl")
|
||||
},
|
||||
)
|
||||
defer ts.Close()
|
||||
|
||||
res, err := http.Get(fmt.Sprintf("%s/test", ts.URL))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resp, _ := io.ReadAll(res.Body)
|
||||
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
||||
}
|
||||
|
||||
func TestLoadHTMLFilesReleaseMode(t *testing.T) {
|
||||
ts := setupHTMLFiles(
|
||||
t,
|
||||
ReleaseMode,
|
||||
false,
|
||||
func(router *Engine) {
|
||||
router.LoadHTMLFiles("./testdata/template/hello.tmpl", "./testdata/template/raw.tmpl")
|
||||
},
|
||||
)
|
||||
defer ts.Close()
|
||||
|
||||
res, err := http.Get(fmt.Sprintf("%s/test", ts.URL))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resp, _ := io.ReadAll(res.Body)
|
||||
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
||||
}
|
||||
|
||||
func TestLoadHTMLFilesUsingTLS(t *testing.T) {
|
||||
ts := setupHTMLFiles(
|
||||
t,
|
||||
TestMode,
|
||||
true,
|
||||
func(router *Engine) {
|
||||
router.LoadHTMLFiles("./testdata/template/hello.tmpl", "./testdata/template/raw.tmpl")
|
||||
},
|
||||
)
|
||||
defer ts.Close()
|
||||
|
||||
// Use InsecureSkipVerify for avoiding `x509: certificate signed by unknown authority` error
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
}
|
||||
client := &http.Client{Transport: tr}
|
||||
res, err := client.Get(fmt.Sprintf("%s/test", ts.URL))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resp, _ := io.ReadAll(res.Body)
|
||||
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
||||
}
|
||||
|
||||
func TestLoadHTMLFilesFuncMap(t *testing.T) {
|
||||
ts := setupHTMLFiles(
|
||||
t,
|
||||
TestMode,
|
||||
false,
|
||||
func(router *Engine) {
|
||||
router.LoadHTMLFiles("./testdata/template/hello.tmpl", "./testdata/template/raw.tmpl")
|
||||
},
|
||||
)
|
||||
defer ts.Close()
|
||||
|
||||
res, err := http.Get(fmt.Sprintf("%s/raw", ts.URL))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resp, _ := io.ReadAll(res.Body)
|
||||
assert.Equal(t, "Date: 2017/07/01", string(resp))
|
||||
}
|
||||
|
||||
func TestAddRoute(t *testing.T) {
|
||||
router := New()
|
||||
router.addRoute("GET", "/", HandlersChain{func(_ *Context) {}})
|
||||
|
||||
assert.Len(t, router.trees, 1)
|
||||
assert.NotNil(t, router.trees.get("GET"))
|
||||
assert.Nil(t, router.trees.get("POST"))
|
||||
|
||||
router.addRoute("POST", "/", HandlersChain{func(_ *Context) {}})
|
||||
|
||||
assert.Len(t, router.trees, 2)
|
||||
assert.NotNil(t, router.trees.get("GET"))
|
||||
assert.NotNil(t, router.trees.get("POST"))
|
||||
|
||||
router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
|
||||
assert.Len(t, router.trees, 2)
|
||||
}
|
||||
|
||||
func TestAddRouteFails(t *testing.T) {
|
||||
router := New()
|
||||
assert.Panics(t, func() { router.addRoute("", "/", HandlersChain{func(_ *Context) {}}) })
|
||||
assert.Panics(t, func() { router.addRoute("GET", "a", HandlersChain{func(_ *Context) {}}) })
|
||||
assert.Panics(t, func() { router.addRoute("GET", "/", HandlersChain{}) })
|
||||
|
||||
router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
|
||||
assert.Panics(t, func() {
|
||||
router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
|
||||
})
|
||||
}
|
||||
|
||||
func TestCreateDefaultRouter(t *testing.T) {
|
||||
router := Default()
|
||||
assert.Len(t, router.Handlers, 2)
|
||||
}
|
||||
|
||||
func TestNoRouteWithoutGlobalHandlers(t *testing.T) {
|
||||
var middleware0 HandlerFunc = func(c *Context) {}
|
||||
var middleware1 HandlerFunc = func(c *Context) {}
|
||||
|
||||
router := New()
|
||||
|
||||
router.NoRoute(middleware0)
|
||||
assert.Nil(t, router.Handlers)
|
||||
assert.Len(t, router.noRoute, 1)
|
||||
assert.Len(t, router.allNoRoute, 1)
|
||||
compareFunc(t, router.noRoute[0], middleware0)
|
||||
compareFunc(t, router.allNoRoute[0], middleware0)
|
||||
|
||||
router.NoRoute(middleware1, middleware0)
|
||||
assert.Len(t, router.noRoute, 2)
|
||||
assert.Len(t, router.allNoRoute, 2)
|
||||
compareFunc(t, router.noRoute[0], middleware1)
|
||||
compareFunc(t, router.allNoRoute[0], middleware1)
|
||||
compareFunc(t, router.noRoute[1], middleware0)
|
||||
compareFunc(t, router.allNoRoute[1], middleware0)
|
||||
}
|
||||
|
||||
func TestNoRouteWithGlobalHandlers(t *testing.T) {
|
||||
var middleware0 HandlerFunc = func(c *Context) {}
|
||||
var middleware1 HandlerFunc = func(c *Context) {}
|
||||
var middleware2 HandlerFunc = func(c *Context) {}
|
||||
|
||||
router := New()
|
||||
router.Use(middleware2)
|
||||
|
||||
router.NoRoute(middleware0)
|
||||
assert.Len(t, router.allNoRoute, 2)
|
||||
assert.Len(t, router.Handlers, 1)
|
||||
assert.Len(t, router.noRoute, 1)
|
||||
|
||||
compareFunc(t, router.Handlers[0], middleware2)
|
||||
compareFunc(t, router.noRoute[0], middleware0)
|
||||
compareFunc(t, router.allNoRoute[0], middleware2)
|
||||
compareFunc(t, router.allNoRoute[1], middleware0)
|
||||
|
||||
router.Use(middleware1)
|
||||
assert.Len(t, router.allNoRoute, 3)
|
||||
assert.Len(t, router.Handlers, 2)
|
||||
assert.Len(t, router.noRoute, 1)
|
||||
|
||||
compareFunc(t, router.Handlers[0], middleware2)
|
||||
compareFunc(t, router.Handlers[1], middleware1)
|
||||
compareFunc(t, router.noRoute[0], middleware0)
|
||||
compareFunc(t, router.allNoRoute[0], middleware2)
|
||||
compareFunc(t, router.allNoRoute[1], middleware1)
|
||||
compareFunc(t, router.allNoRoute[2], middleware0)
|
||||
}
|
||||
|
||||
func TestNoMethodWithoutGlobalHandlers(t *testing.T) {
|
||||
var middleware0 HandlerFunc = func(c *Context) {}
|
||||
var middleware1 HandlerFunc = func(c *Context) {}
|
||||
|
||||
router := New()
|
||||
|
||||
router.NoMethod(middleware0)
|
||||
assert.Empty(t, router.Handlers)
|
||||
assert.Len(t, router.noMethod, 1)
|
||||
assert.Len(t, router.allNoMethod, 1)
|
||||
compareFunc(t, router.noMethod[0], middleware0)
|
||||
compareFunc(t, router.allNoMethod[0], middleware0)
|
||||
|
||||
router.NoMethod(middleware1, middleware0)
|
||||
assert.Len(t, router.noMethod, 2)
|
||||
assert.Len(t, router.allNoMethod, 2)
|
||||
compareFunc(t, router.noMethod[0], middleware1)
|
||||
compareFunc(t, router.allNoMethod[0], middleware1)
|
||||
compareFunc(t, router.noMethod[1], middleware0)
|
||||
compareFunc(t, router.allNoMethod[1], middleware0)
|
||||
}
|
||||
|
||||
func TestRebuild404Handlers(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNoMethodWithGlobalHandlers(t *testing.T) {
|
||||
var middleware0 HandlerFunc = func(c *Context) {}
|
||||
var middleware1 HandlerFunc = func(c *Context) {}
|
||||
var middleware2 HandlerFunc = func(c *Context) {}
|
||||
|
||||
router := New()
|
||||
router.Use(middleware2)
|
||||
|
||||
router.NoMethod(middleware0)
|
||||
assert.Len(t, router.allNoMethod, 2)
|
||||
assert.Len(t, router.Handlers, 1)
|
||||
assert.Len(t, router.noMethod, 1)
|
||||
|
||||
compareFunc(t, router.Handlers[0], middleware2)
|
||||
compareFunc(t, router.noMethod[0], middleware0)
|
||||
compareFunc(t, router.allNoMethod[0], middleware2)
|
||||
compareFunc(t, router.allNoMethod[1], middleware0)
|
||||
|
||||
router.Use(middleware1)
|
||||
assert.Len(t, router.allNoMethod, 3)
|
||||
assert.Len(t, router.Handlers, 2)
|
||||
assert.Len(t, router.noMethod, 1)
|
||||
|
||||
compareFunc(t, router.Handlers[0], middleware2)
|
||||
compareFunc(t, router.Handlers[1], middleware1)
|
||||
compareFunc(t, router.noMethod[0], middleware0)
|
||||
compareFunc(t, router.allNoMethod[0], middleware2)
|
||||
compareFunc(t, router.allNoMethod[1], middleware1)
|
||||
compareFunc(t, router.allNoMethod[2], middleware0)
|
||||
}
|
||||
|
||||
func compareFunc(t *testing.T, a, b any) {
|
||||
sf1 := reflect.ValueOf(a)
|
||||
sf2 := reflect.ValueOf(b)
|
||||
if sf1.Pointer() != sf2.Pointer() {
|
||||
t.Error("different functions")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListOfRoutes(t *testing.T) {
|
||||
router := New()
|
||||
router.GET("/favicon.ico", handlerTest1)
|
||||
router.GET("/", handlerTest1)
|
||||
group := router.Group("/users")
|
||||
{
|
||||
group.GET("/", handlerTest2)
|
||||
group.GET("/:id", handlerTest1)
|
||||
group.POST("/:id", handlerTest2)
|
||||
}
|
||||
router.Static("/static", ".")
|
||||
|
||||
list := router.Routes()
|
||||
|
||||
assert.Len(t, list, 7)
|
||||
assertRoutePresent(t, list, RouteInfo{
|
||||
Method: "GET",
|
||||
Path: "/favicon.ico",
|
||||
Handler: "^(.*/vendor/)?git.internal/re/gin.handlerTest1$",
|
||||
})
|
||||
assertRoutePresent(t, list, RouteInfo{
|
||||
Method: "GET",
|
||||
Path: "/",
|
||||
Handler: "^(.*/vendor/)?git.internal/re/gin.handlerTest1$",
|
||||
})
|
||||
assertRoutePresent(t, list, RouteInfo{
|
||||
Method: "GET",
|
||||
Path: "/users/",
|
||||
Handler: "^(.*/vendor/)?git.internal/re/gin.handlerTest2$",
|
||||
})
|
||||
assertRoutePresent(t, list, RouteInfo{
|
||||
Method: "GET",
|
||||
Path: "/users/:id",
|
||||
Handler: "^(.*/vendor/)?git.internal/re/gin.handlerTest1$",
|
||||
})
|
||||
assertRoutePresent(t, list, RouteInfo{
|
||||
Method: "POST",
|
||||
Path: "/users/:id",
|
||||
Handler: "^(.*/vendor/)?git.internal/re/gin.handlerTest2$",
|
||||
})
|
||||
}
|
||||
|
||||
func TestEngineHandleContext(t *testing.T) {
|
||||
r := New()
|
||||
r.GET("/", func(c *Context) {
|
||||
c.Request.URL.Path = "/v2"
|
||||
r.HandleContext(c)
|
||||
})
|
||||
v2 := r.Group("/v2")
|
||||
{
|
||||
v2.GET("/", func(c *Context) {})
|
||||
}
|
||||
|
||||
assert.NotPanics(t, func() {
|
||||
w := PerformRequest(r, "GET", "/")
|
||||
assert.Equal(t, 301, w.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEngineHandleContextManyReEntries(t *testing.T) {
|
||||
expectValue := 10000
|
||||
|
||||
var handlerCounter, middlewareCounter int64
|
||||
|
||||
r := New()
|
||||
r.Use(func(c *Context) {
|
||||
atomic.AddInt64(&middlewareCounter, 1)
|
||||
})
|
||||
r.GET("/:count", func(c *Context) {
|
||||
countStr := c.Param("count")
|
||||
count, err := strconv.Atoi(countStr)
|
||||
assert.NoError(t, err)
|
||||
|
||||
n, err := c.Writer.Write([]byte("."))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, n)
|
||||
|
||||
switch {
|
||||
case count > 0:
|
||||
c.Request.URL.Path = "/" + strconv.Itoa(count-1)
|
||||
r.HandleContext(c)
|
||||
}
|
||||
}, func(c *Context) {
|
||||
atomic.AddInt64(&handlerCounter, 1)
|
||||
})
|
||||
|
||||
assert.NotPanics(t, func() {
|
||||
w := PerformRequest(r, "GET", "/"+strconv.Itoa(expectValue-1)) // include 0 value
|
||||
assert.Equal(t, 200, w.Code)
|
||||
assert.Equal(t, expectValue, w.Body.Len())
|
||||
})
|
||||
|
||||
assert.Equal(t, int64(expectValue), handlerCounter)
|
||||
assert.Equal(t, int64(expectValue), middlewareCounter)
|
||||
}
|
||||
|
||||
func TestPrepareTrustedCIRDsWith(t *testing.T) {
|
||||
r := New()
|
||||
|
||||
// valid ipv4 cidr
|
||||
{
|
||||
expectedTrustedCIDRs := []*net.IPNet{parseCIDR("0.0.0.0/0")}
|
||||
err := r.SetTrustedProxies([]string{"0.0.0.0/0"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedTrustedCIDRs, r.trustedCIDRs)
|
||||
}
|
||||
|
||||
// invalid ipv4 cidr
|
||||
{
|
||||
err := r.SetTrustedProxies([]string{"192.168.1.33/33"})
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
// valid ipv4 address
|
||||
{
|
||||
expectedTrustedCIDRs := []*net.IPNet{parseCIDR("192.168.1.33/32")}
|
||||
|
||||
err := r.SetTrustedProxies([]string{"192.168.1.33"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedTrustedCIDRs, r.trustedCIDRs)
|
||||
}
|
||||
|
||||
// invalid ipv4 address
|
||||
{
|
||||
err := r.SetTrustedProxies([]string{"192.168.1.256"})
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
// valid ipv6 address
|
||||
{
|
||||
expectedTrustedCIDRs := []*net.IPNet{parseCIDR("2002:0000:0000:1234:abcd:ffff:c0a8:0101/128")}
|
||||
err := r.SetTrustedProxies([]string{"2002:0000:0000:1234:abcd:ffff:c0a8:0101"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedTrustedCIDRs, r.trustedCIDRs)
|
||||
}
|
||||
|
||||
// invalid ipv6 address
|
||||
{
|
||||
err := r.SetTrustedProxies([]string{"gggg:0000:0000:1234:abcd:ffff:c0a8:0101"})
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
// valid ipv6 cidr
|
||||
{
|
||||
expectedTrustedCIDRs := []*net.IPNet{parseCIDR("::/0")}
|
||||
err := r.SetTrustedProxies([]string{"::/0"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedTrustedCIDRs, r.trustedCIDRs)
|
||||
}
|
||||
|
||||
// invalid ipv6 cidr
|
||||
{
|
||||
err := r.SetTrustedProxies([]string{"gggg:0000:0000:1234:abcd:ffff:c0a8:0101/129"})
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
// valid combination
|
||||
{
|
||||
expectedTrustedCIDRs := []*net.IPNet{
|
||||
parseCIDR("::/0"),
|
||||
parseCIDR("192.168.0.0/16"),
|
||||
parseCIDR("172.16.0.1/32"),
|
||||
}
|
||||
err := r.SetTrustedProxies([]string{
|
||||
"::/0",
|
||||
"192.168.0.0/16",
|
||||
"172.16.0.1",
|
||||
})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedTrustedCIDRs, r.trustedCIDRs)
|
||||
}
|
||||
|
||||
// invalid combination
|
||||
{
|
||||
err := r.SetTrustedProxies([]string{
|
||||
"::/0",
|
||||
"192.168.0.0/16",
|
||||
"172.16.0.256",
|
||||
})
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
// nil value
|
||||
{
|
||||
err := r.SetTrustedProxies(nil)
|
||||
|
||||
assert.Nil(t, r.trustedCIDRs)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
func parseCIDR(cidr string) *net.IPNet {
|
||||
_, parsedCIDR, err := net.ParseCIDR(cidr)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
return parsedCIDR
|
||||
}
|
||||
|
||||
func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo) {
|
||||
for _, gotRoute := range gotRoutes {
|
||||
if gotRoute.Path == wantRoute.Path && gotRoute.Method == wantRoute.Method {
|
||||
assert.Regexp(t, wantRoute.Handler, gotRoute.Handler)
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Errorf("route not found: %v", wantRoute)
|
||||
}
|
||||
|
||||
func handlerTest1(c *Context) {}
|
||||
func handlerTest2(c *Context) {}
|
|
@ -1,474 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type route struct {
|
||||
method string
|
||||
path string
|
||||
}
|
||||
|
||||
// http://developer.github.com/v3/
|
||||
var githubAPI = []route{
|
||||
// OAuth Authorizations
|
||||
{http.MethodGet, "/authorizations"},
|
||||
{http.MethodGet, "/authorizations/:id"},
|
||||
{http.MethodPost, "/authorizations"},
|
||||
//{http.MethodPut, "/authorizations/clients/:client_id"},
|
||||
//{http.MethodPatch, "/authorizations/:id"},
|
||||
{http.MethodDelete, "/authorizations/:id"},
|
||||
{http.MethodGet, "/applications/:client_id/tokens/:access_token"},
|
||||
{http.MethodDelete, "/applications/:client_id/tokens"},
|
||||
{http.MethodDelete, "/applications/:client_id/tokens/:access_token"},
|
||||
|
||||
// Activity
|
||||
{http.MethodGet, "/events"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/events"},
|
||||
{http.MethodGet, "/networks/:owner/:repo/events"},
|
||||
{http.MethodGet, "/orgs/:org/events"},
|
||||
{http.MethodGet, "/users/:user/received_events"},
|
||||
{http.MethodGet, "/users/:user/received_events/public"},
|
||||
{http.MethodGet, "/users/:user/events"},
|
||||
{http.MethodGet, "/users/:user/events/public"},
|
||||
{http.MethodGet, "/users/:user/events/orgs/:org"},
|
||||
{http.MethodGet, "/feeds"},
|
||||
{http.MethodGet, "/notifications"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/notifications"},
|
||||
{http.MethodPut, "/notifications"},
|
||||
{http.MethodPut, "/repos/:owner/:repo/notifications"},
|
||||
{http.MethodGet, "/notifications/threads/:id"},
|
||||
//{http.MethodPatch, "/notifications/threads/:id"},
|
||||
{http.MethodGet, "/notifications/threads/:id/subscription"},
|
||||
{http.MethodPut, "/notifications/threads/:id/subscription"},
|
||||
{http.MethodDelete, "/notifications/threads/:id/subscription"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/stargazers"},
|
||||
{http.MethodGet, "/users/:user/starred"},
|
||||
{http.MethodGet, "/user/starred"},
|
||||
{http.MethodGet, "/user/starred/:owner/:repo"},
|
||||
{http.MethodPut, "/user/starred/:owner/:repo"},
|
||||
{http.MethodDelete, "/user/starred/:owner/:repo"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/subscribers"},
|
||||
{http.MethodGet, "/users/:user/subscriptions"},
|
||||
{http.MethodGet, "/user/subscriptions"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/subscription"},
|
||||
{http.MethodPut, "/repos/:owner/:repo/subscription"},
|
||||
{http.MethodDelete, "/repos/:owner/:repo/subscription"},
|
||||
{http.MethodGet, "/user/subscriptions/:owner/:repo"},
|
||||
{http.MethodPut, "/user/subscriptions/:owner/:repo"},
|
||||
{http.MethodDelete, "/user/subscriptions/:owner/:repo"},
|
||||
|
||||
// Gists
|
||||
{http.MethodGet, "/users/:user/gists"},
|
||||
{http.MethodGet, "/gists"},
|
||||
//{http.MethodGet, "/gists/public"},
|
||||
//{http.MethodGet, "/gists/starred"},
|
||||
{http.MethodGet, "/gists/:id"},
|
||||
{http.MethodPost, "/gists"},
|
||||
//{http.MethodPatch, "/gists/:id"},
|
||||
{http.MethodPut, "/gists/:id/star"},
|
||||
{http.MethodDelete, "/gists/:id/star"},
|
||||
{http.MethodGet, "/gists/:id/star"},
|
||||
{http.MethodPost, "/gists/:id/forks"},
|
||||
{http.MethodDelete, "/gists/:id"},
|
||||
|
||||
// Git Data
|
||||
{http.MethodGet, "/repos/:owner/:repo/git/blobs/:sha"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/git/blobs"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/git/commits/:sha"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/git/commits"},
|
||||
//{http.MethodGet, "/repos/:owner/:repo/git/refs/*ref"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/git/refs"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/git/refs"},
|
||||
//{http.MethodPatch, "/repos/:owner/:repo/git/refs/*ref"},
|
||||
//{http.MethodDelete, "/repos/:owner/:repo/git/refs/*ref"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/git/tags/:sha"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/git/tags"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/git/trees/:sha"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/git/trees"},
|
||||
|
||||
// Issues
|
||||
{http.MethodGet, "/issues"},
|
||||
{http.MethodGet, "/user/issues"},
|
||||
{http.MethodGet, "/orgs/:org/issues"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/issues"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/issues/:number"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/issues"},
|
||||
//{http.MethodPatch, "/repos/:owner/:repo/issues/:number"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/assignees"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/assignees/:assignee"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/issues/:number/comments"},
|
||||
//{http.MethodGet, "/repos/:owner/:repo/issues/comments"},
|
||||
//{http.MethodGet, "/repos/:owner/:repo/issues/comments/:id"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/issues/:number/comments"},
|
||||
//{http.MethodPatch, "/repos/:owner/:repo/issues/comments/:id"},
|
||||
//{http.MethodDelete, "/repos/:owner/:repo/issues/comments/:id"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/issues/:number/events"},
|
||||
//{http.MethodGet, "/repos/:owner/:repo/issues/events"},
|
||||
//{http.MethodGet, "/repos/:owner/:repo/issues/events/:id"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/labels"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/labels/:name"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/labels"},
|
||||
//{http.MethodPatch, "/repos/:owner/:repo/labels/:name"},
|
||||
{http.MethodDelete, "/repos/:owner/:repo/labels/:name"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/issues/:number/labels"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/issues/:number/labels"},
|
||||
{http.MethodDelete, "/repos/:owner/:repo/issues/:number/labels/:name"},
|
||||
{http.MethodPut, "/repos/:owner/:repo/issues/:number/labels"},
|
||||
{http.MethodDelete, "/repos/:owner/:repo/issues/:number/labels"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/milestones/:number/labels"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/milestones"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/milestones/:number"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/milestones"},
|
||||
//{http.MethodPatch, "/repos/:owner/:repo/milestones/:number"},
|
||||
{http.MethodDelete, "/repos/:owner/:repo/milestones/:number"},
|
||||
|
||||
// Miscellaneous
|
||||
{http.MethodGet, "/emojis"},
|
||||
{http.MethodGet, "/gitignore/templates"},
|
||||
{http.MethodGet, "/gitignore/templates/:name"},
|
||||
{http.MethodPost, "/markdown"},
|
||||
{http.MethodPost, "/markdown/raw"},
|
||||
{http.MethodGet, "/meta"},
|
||||
{http.MethodGet, "/rate_limit"},
|
||||
|
||||
// Organizations
|
||||
{http.MethodGet, "/users/:user/orgs"},
|
||||
{http.MethodGet, "/user/orgs"},
|
||||
{http.MethodGet, "/orgs/:org"},
|
||||
//{http.MethodPatch, "/orgs/:org"},
|
||||
{http.MethodGet, "/orgs/:org/members"},
|
||||
{http.MethodGet, "/orgs/:org/members/:user"},
|
||||
{http.MethodDelete, "/orgs/:org/members/:user"},
|
||||
{http.MethodGet, "/orgs/:org/public_members"},
|
||||
{http.MethodGet, "/orgs/:org/public_members/:user"},
|
||||
{http.MethodPut, "/orgs/:org/public_members/:user"},
|
||||
{http.MethodDelete, "/orgs/:org/public_members/:user"},
|
||||
{http.MethodGet, "/orgs/:org/teams"},
|
||||
{http.MethodGet, "/teams/:id"},
|
||||
{http.MethodPost, "/orgs/:org/teams"},
|
||||
//{http.MethodPatch, "/teams/:id"},
|
||||
{http.MethodDelete, "/teams/:id"},
|
||||
{http.MethodGet, "/teams/:id/members"},
|
||||
{http.MethodGet, "/teams/:id/members/:user"},
|
||||
{http.MethodPut, "/teams/:id/members/:user"},
|
||||
{http.MethodDelete, "/teams/:id/members/:user"},
|
||||
{http.MethodGet, "/teams/:id/repos"},
|
||||
{http.MethodGet, "/teams/:id/repos/:owner/:repo"},
|
||||
{http.MethodPut, "/teams/:id/repos/:owner/:repo"},
|
||||
{http.MethodDelete, "/teams/:id/repos/:owner/:repo"},
|
||||
{http.MethodGet, "/user/teams"},
|
||||
|
||||
// Pull Requests
|
||||
{http.MethodGet, "/repos/:owner/:repo/pulls"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/pulls/:number"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/pulls"},
|
||||
//{http.MethodPatch, "/repos/:owner/:repo/pulls/:number"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/pulls/:number/commits"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/pulls/:number/files"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/pulls/:number/merge"},
|
||||
{http.MethodPut, "/repos/:owner/:repo/pulls/:number/merge"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/pulls/:number/comments"},
|
||||
//{http.MethodGet, "/repos/:owner/:repo/pulls/comments"},
|
||||
//{http.MethodGet, "/repos/:owner/:repo/pulls/comments/:number"},
|
||||
{http.MethodPut, "/repos/:owner/:repo/pulls/:number/comments"},
|
||||
//{http.MethodPatch, "/repos/:owner/:repo/pulls/comments/:number"},
|
||||
//{http.MethodDelete, "/repos/:owner/:repo/pulls/comments/:number"},
|
||||
|
||||
// Repositories
|
||||
{http.MethodGet, "/user/repos"},
|
||||
{http.MethodGet, "/users/:user/repos"},
|
||||
{http.MethodGet, "/orgs/:org/repos"},
|
||||
{http.MethodGet, "/repositories"},
|
||||
{http.MethodPost, "/user/repos"},
|
||||
{http.MethodPost, "/orgs/:org/repos"},
|
||||
{http.MethodGet, "/repos/:owner/:repo"},
|
||||
//{http.MethodPatch, "/repos/:owner/:repo"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/contributors"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/languages"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/teams"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/tags"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/branches"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/branches/:branch"},
|
||||
{http.MethodDelete, "/repos/:owner/:repo"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/collaborators"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/collaborators/:user"},
|
||||
{http.MethodPut, "/repos/:owner/:repo/collaborators/:user"},
|
||||
{http.MethodDelete, "/repos/:owner/:repo/collaborators/:user"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/comments"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/commits/:sha/comments"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/commits/:sha/comments"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/comments/:id"},
|
||||
//{http.MethodPatch, "/repos/:owner/:repo/comments/:id"},
|
||||
{http.MethodDelete, "/repos/:owner/:repo/comments/:id"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/commits"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/commits/:sha"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/readme"},
|
||||
//{http.MethodGet, "/repos/:owner/:repo/contents/*path"},
|
||||
//{http.MethodPut, "/repos/:owner/:repo/contents/*path"},
|
||||
//{http.MethodDelete, "/repos/:owner/:repo/contents/*path"},
|
||||
//{http.MethodGet, "/repos/:owner/:repo/:archive_format/:ref"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/keys"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/keys/:id"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/keys"},
|
||||
//{http.MethodPatch, "/repos/:owner/:repo/keys/:id"},
|
||||
{http.MethodDelete, "/repos/:owner/:repo/keys/:id"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/downloads"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/downloads/:id"},
|
||||
{http.MethodDelete, "/repos/:owner/:repo/downloads/:id"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/forks"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/forks"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/hooks"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/hooks/:id"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/hooks"},
|
||||
//{http.MethodPatch, "/repos/:owner/:repo/hooks/:id"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/hooks/:id/tests"},
|
||||
{http.MethodDelete, "/repos/:owner/:repo/hooks/:id"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/merges"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/releases"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/releases/:id"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/releases"},
|
||||
//{http.MethodPatch, "/repos/:owner/:repo/releases/:id"},
|
||||
{http.MethodDelete, "/repos/:owner/:repo/releases/:id"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/releases/:id/assets"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/stats/contributors"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/stats/commit_activity"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/stats/code_frequency"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/stats/participation"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/stats/punch_card"},
|
||||
{http.MethodGet, "/repos/:owner/:repo/statuses/:ref"},
|
||||
{http.MethodPost, "/repos/:owner/:repo/statuses/:ref"},
|
||||
|
||||
// Search
|
||||
{http.MethodGet, "/search/repositories"},
|
||||
{http.MethodGet, "/search/code"},
|
||||
{http.MethodGet, "/search/issues"},
|
||||
{http.MethodGet, "/search/users"},
|
||||
{http.MethodGet, "/legacy/issues/search/:owner/:repository/:state/:keyword"},
|
||||
{http.MethodGet, "/legacy/repos/search/:keyword"},
|
||||
{http.MethodGet, "/legacy/user/search/:keyword"},
|
||||
{http.MethodGet, "/legacy/user/email/:email"},
|
||||
|
||||
// Users
|
||||
{http.MethodGet, "/users/:user"},
|
||||
{http.MethodGet, "/user"},
|
||||
//{http.MethodPatch, "/user"},
|
||||
{http.MethodGet, "/users"},
|
||||
{http.MethodGet, "/user/emails"},
|
||||
{http.MethodPost, "/user/emails"},
|
||||
{http.MethodDelete, "/user/emails"},
|
||||
{http.MethodGet, "/users/:user/followers"},
|
||||
{http.MethodGet, "/user/followers"},
|
||||
{http.MethodGet, "/users/:user/following"},
|
||||
{http.MethodGet, "/user/following"},
|
||||
{http.MethodGet, "/user/following/:user"},
|
||||
{http.MethodGet, "/users/:user/following/:target_user"},
|
||||
{http.MethodPut, "/user/following/:user"},
|
||||
{http.MethodDelete, "/user/following/:user"},
|
||||
{http.MethodGet, "/users/:user/keys"},
|
||||
{http.MethodGet, "/user/keys"},
|
||||
{http.MethodGet, "/user/keys/:id"},
|
||||
{http.MethodPost, "/user/keys"},
|
||||
//{http.MethodPatch, "/user/keys/:id"},
|
||||
{http.MethodDelete, "/user/keys/:id"},
|
||||
}
|
||||
|
||||
func TestShouldBindUri(t *testing.T) {
|
||||
DefaultWriter = os.Stdout
|
||||
router := New()
|
||||
|
||||
type Person struct {
|
||||
Name string `uri:"name" binding:"required"`
|
||||
ID string `uri:"id" binding:"required"`
|
||||
}
|
||||
router.Handle(http.MethodGet, "/rest/:name/:id", func(c *Context) {
|
||||
var person Person
|
||||
assert.NoError(t, c.ShouldBindUri(&person))
|
||||
assert.True(t, person.Name != "")
|
||||
assert.True(t, person.ID != "")
|
||||
c.String(http.StatusOK, "ShouldBindUri test OK")
|
||||
})
|
||||
|
||||
path, _ := exampleFromPath("/rest/:name/:id")
|
||||
w := PerformRequest(router, http.MethodGet, path)
|
||||
assert.Equal(t, "ShouldBindUri test OK", w.Body.String())
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
func TestBindUri(t *testing.T) {
|
||||
DefaultWriter = os.Stdout
|
||||
router := New()
|
||||
|
||||
type Person struct {
|
||||
Name string `uri:"name" binding:"required"`
|
||||
ID string `uri:"id" binding:"required"`
|
||||
}
|
||||
router.Handle(http.MethodGet, "/rest/:name/:id", func(c *Context) {
|
||||
var person Person
|
||||
assert.NoError(t, c.BindUri(&person))
|
||||
assert.True(t, person.Name != "")
|
||||
assert.True(t, person.ID != "")
|
||||
c.String(http.StatusOK, "BindUri test OK")
|
||||
})
|
||||
|
||||
path, _ := exampleFromPath("/rest/:name/:id")
|
||||
w := PerformRequest(router, http.MethodGet, path)
|
||||
assert.Equal(t, "BindUri test OK", w.Body.String())
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
func TestBindUriError(t *testing.T) {
|
||||
DefaultWriter = os.Stdout
|
||||
router := New()
|
||||
|
||||
type Member struct {
|
||||
Number string `uri:"num" binding:"required,uuid"`
|
||||
}
|
||||
router.Handle(http.MethodGet, "/new/rest/:num", func(c *Context) {
|
||||
var m Member
|
||||
assert.Error(t, c.BindUri(&m))
|
||||
})
|
||||
|
||||
path1, _ := exampleFromPath("/new/rest/:num")
|
||||
w1 := PerformRequest(router, http.MethodGet, path1)
|
||||
assert.Equal(t, http.StatusBadRequest, w1.Code)
|
||||
}
|
||||
|
||||
func TestRaceContextCopy(t *testing.T) {
|
||||
DefaultWriter = os.Stdout
|
||||
router := Default()
|
||||
router.GET("/test/copy/race", func(c *Context) {
|
||||
c.Set("1", 0)
|
||||
c.Set("2", 0)
|
||||
|
||||
// Sending a copy of the Context to two separate routines
|
||||
go readWriteKeys(c.Copy())
|
||||
go readWriteKeys(c.Copy())
|
||||
c.String(http.StatusOK, "run OK, no panics")
|
||||
})
|
||||
w := PerformRequest(router, http.MethodGet, "/test/copy/race")
|
||||
assert.Equal(t, "run OK, no panics", w.Body.String())
|
||||
}
|
||||
|
||||
func readWriteKeys(c *Context) {
|
||||
for {
|
||||
c.Set("1", rand.Int())
|
||||
c.Set("2", c.Value("1"))
|
||||
}
|
||||
}
|
||||
|
||||
func githubConfigRouter(router *Engine) {
|
||||
for _, route := range githubAPI {
|
||||
router.Handle(route.method, route.path, func(c *Context) {
|
||||
output := make(map[string]string, len(c.Params)+1)
|
||||
output["status"] = "good"
|
||||
for _, param := range c.Params {
|
||||
output[param.Key] = param.Value
|
||||
}
|
||||
c.JSON(http.StatusOK, output)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGithubAPI(t *testing.T) {
|
||||
DefaultWriter = os.Stdout
|
||||
router := New()
|
||||
githubConfigRouter(router)
|
||||
|
||||
for _, route := range githubAPI {
|
||||
path, values := exampleFromPath(route.path)
|
||||
w := PerformRequest(router, route.method, path)
|
||||
|
||||
// TEST
|
||||
assert.Contains(t, w.Body.String(), "\"status\":\"good\"")
|
||||
for _, value := range values {
|
||||
str := fmt.Sprintf("\"%s\":\"%s\"", value.Key, value.Value)
|
||||
assert.Contains(t, w.Body.String(), str)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func exampleFromPath(path string) (string, Params) {
|
||||
output := new(bytes.Buffer)
|
||||
params := make(Params, 0, 6)
|
||||
start := -1
|
||||
for i, c := range path {
|
||||
if c == ':' {
|
||||
start = i + 1
|
||||
}
|
||||
if start >= 0 {
|
||||
if c == '/' {
|
||||
value := fmt.Sprint(rand.Intn(100000))
|
||||
params = append(params, Param{
|
||||
Key: path[start:i],
|
||||
Value: value,
|
||||
})
|
||||
output.WriteString(value)
|
||||
output.WriteRune(c)
|
||||
start = -1
|
||||
}
|
||||
} else {
|
||||
output.WriteRune(c)
|
||||
}
|
||||
}
|
||||
if start >= 0 {
|
||||
value := fmt.Sprint(rand.Intn(100000))
|
||||
params = append(params, Param{
|
||||
Key: path[start:],
|
||||
Value: value,
|
||||
})
|
||||
output.WriteString(value)
|
||||
}
|
||||
|
||||
return output.String(), params
|
||||
}
|
||||
|
||||
func BenchmarkGithub(b *testing.B) {
|
||||
router := New()
|
||||
githubConfigRouter(router)
|
||||
runRequest(b, router, http.MethodGet, "/legacy/issues/search/:owner/:repository/:state/:keyword")
|
||||
}
|
||||
|
||||
func BenchmarkParallelGithub(b *testing.B) {
|
||||
DefaultWriter = os.Stdout
|
||||
router := New()
|
||||
githubConfigRouter(router)
|
||||
|
||||
req, _ := http.NewRequest(http.MethodPost, "/repos/manucorporat/sse/git/blobs", nil)
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
// Each goroutine has its own bytes.Buffer.
|
||||
for pb.Next() {
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkParallelGithubDefault(b *testing.B) {
|
||||
DefaultWriter = os.Stdout
|
||||
router := New()
|
||||
githubConfigRouter(router)
|
||||
|
||||
req, _ := http.NewRequest(http.MethodPost, "/repos/manucorporat/sse/git/blobs", nil)
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
// Each goroutine has its own bytes.Buffer.
|
||||
for pb.Next() {
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
}
|
||||
})
|
||||
}
|
35
go.mod
35
go.mod
|
@ -1,35 +0,0 @@
|
|||
module git.internal/re/gin
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/bytedance/sonic v1.6.1
|
||||
github.com/gin-contrib/sse v0.1.0
|
||||
github.com/go-playground/validator/v10 v10.11.1
|
||||
github.com/goccy/go-json v0.10.0
|
||||
github.com/json-iterator/go v1.1.12
|
||||
github.com/mattn/go-isatty v0.0.17
|
||||
github.com/pelletier/go-toml/v2 v2.0.6
|
||||
github.com/stretchr/testify v1.8.1
|
||||
github.com/ugorji/go/codec v1.2.8
|
||||
golang.org/x/net v0.5.0
|
||||
google.golang.org/protobuf v1.28.1
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/go-playground/locales v0.14.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.0.14 // indirect
|
||||
github.com/leodido/go-urn v1.2.1 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
golang.org/x/arch v0.0.0-20220412001346-fc48f9fe4c15 // indirect
|
||||
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
|
||||
golang.org/x/sys v0.4.0 // indirect
|
||||
golang.org/x/text v0.6.0 // indirect
|
||||
)
|
105
go.sum
105
go.sum
|
@ -1,105 +0,0 @@
|
|||
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
|
||||
github.com/bytedance/sonic v1.6.1 h1:HEyWqlvEh95R/rMg5Mh6jDx5Zt35MG24QWzpHMVuan0=
|
||||
github.com/bytedance/sonic v1.6.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
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/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
|
||||
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
|
||||
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
|
||||
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
|
||||
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
|
||||
github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ=
|
||||
github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
|
||||
github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA=
|
||||
github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.0.14 h1:QRqdp6bb9M9S5yyKeYteXKuoKE4p0tGlra81fKOpWH8=
|
||||
github.com/klauspost/cpuid/v2 v2.0.14/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
|
||||
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
|
||||
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
|
||||
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
|
||||
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||
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/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
|
||||
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.8 h1:sgBJS6COt0b/P40VouWKdseidkDgHxYGm0SAglUHfP0=
|
||||
github.com/ugorji/go/codec v1.2.8/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/arch v0.0.0-20220412001346-fc48f9fe4c15 h1:GVfVkciLYxn5mY5EncwAe0SXUn9Rm81rRkZ0TTmn/cU=
|
||||
golang.org/x/arch v0.0.0-20220412001346-fc48f9fe4c15/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
|
||||
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw=
|
||||
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
|
||||
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
|
||||
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
|
||||
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
|
@ -1,24 +0,0 @@
|
|||
// Copyright 2020 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package bytesconv
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// StringToBytes converts string to byte slice without a memory allocation.
|
||||
func StringToBytes(s string) []byte {
|
||||
return *(*[]byte)(unsafe.Pointer(
|
||||
&struct {
|
||||
string
|
||||
Cap int
|
||||
}{s, len(s)},
|
||||
))
|
||||
}
|
||||
|
||||
// BytesToString converts byte slice to string without a memory allocation.
|
||||
func BytesToString(b []byte) string {
|
||||
return *(*string)(unsafe.Pointer(&b))
|
||||
}
|
|
@ -1,99 +0,0 @@
|
|||
// Copyright 2020 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package bytesconv
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var testString = "Albert Einstein: Logic will get you from A to B. Imagination will take you everywhere."
|
||||
var testBytes = []byte(testString)
|
||||
|
||||
func rawBytesToStr(b []byte) string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func rawStrToBytes(s string) []byte {
|
||||
return []byte(s)
|
||||
}
|
||||
|
||||
// go test -v
|
||||
|
||||
func TestBytesToString(t *testing.T) {
|
||||
data := make([]byte, 1024)
|
||||
for i := 0; i < 100; i++ {
|
||||
rand.Read(data)
|
||||
if rawBytesToStr(data) != BytesToString(data) {
|
||||
t.Fatal("don't match")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
const (
|
||||
letterIdxBits = 6 // 6 bits to represent a letter index
|
||||
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
|
||||
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
|
||||
)
|
||||
|
||||
var src = rand.NewSource(time.Now().UnixNano())
|
||||
|
||||
func RandStringBytesMaskImprSrcSB(n int) string {
|
||||
sb := strings.Builder{}
|
||||
sb.Grow(n)
|
||||
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
|
||||
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
|
||||
if remain == 0 {
|
||||
cache, remain = src.Int63(), letterIdxMax
|
||||
}
|
||||
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
|
||||
sb.WriteByte(letterBytes[idx])
|
||||
i--
|
||||
}
|
||||
cache >>= letterIdxBits
|
||||
remain--
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func TestStringToBytes(t *testing.T) {
|
||||
for i := 0; i < 100; i++ {
|
||||
s := RandStringBytesMaskImprSrcSB(64)
|
||||
if !bytes.Equal(rawStrToBytes(s), StringToBytes(s)) {
|
||||
t.Fatal("don't match")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// go test -v -run=none -bench=^BenchmarkBytesConv -benchmem=true
|
||||
|
||||
func BenchmarkBytesConvBytesToStrRaw(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
rawBytesToStr(testBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkBytesConvBytesToStr(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
BytesToString(testBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkBytesConvStrToBytesRaw(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
rawStrToBytes(testString)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkBytesConvStrToBytes(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
StringToBytes(testString)
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
// Copyright 2017 Bo-Yi Wu. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build go_json
|
||||
// +build go_json
|
||||
|
||||
package json
|
||||
|
||||
import json "github.com/goccy/go-json"
|
||||
|
||||
var (
|
||||
// Marshal is exported by gin/json package.
|
||||
Marshal = json.Marshal
|
||||
// Unmarshal is exported by gin/json package.
|
||||
Unmarshal = json.Unmarshal
|
||||
// MarshalIndent is exported by gin/json package.
|
||||
MarshalIndent = json.MarshalIndent
|
||||
// NewDecoder is exported by gin/json package.
|
||||
NewDecoder = json.NewDecoder
|
||||
// NewEncoder is exported by gin/json package.
|
||||
NewEncoder = json.NewEncoder
|
||||
)
|
|
@ -1,25 +0,0 @@
|
|||
// Copyright 2017 Bo-Yi Wu. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !jsoniter && !go_json && !(sonic && avx && (linux || windows || darwin) && amd64)
|
||||
// +build !jsoniter
|
||||
// +build !go_json
|
||||
// +build !sonic !avx !linux,!windows,!darwin !amd64
|
||||
|
||||
package json
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
var (
|
||||
// Marshal is exported by gin/json package.
|
||||
Marshal = json.Marshal
|
||||
// Unmarshal is exported by gin/json package.
|
||||
Unmarshal = json.Unmarshal
|
||||
// MarshalIndent is exported by gin/json package.
|
||||
MarshalIndent = json.MarshalIndent
|
||||
// NewDecoder is exported by gin/json package.
|
||||
NewDecoder = json.NewDecoder
|
||||
// NewEncoder is exported by gin/json package.
|
||||
NewEncoder = json.NewEncoder
|
||||
)
|
|
@ -1,24 +0,0 @@
|
|||
// Copyright 2017 Bo-Yi Wu. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build jsoniter
|
||||
// +build jsoniter
|
||||
|
||||
package json
|
||||
|
||||
import jsoniter "github.com/json-iterator/go"
|
||||
|
||||
var (
|
||||
json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
// Marshal is exported by gin/json package.
|
||||
Marshal = json.Marshal
|
||||
// Unmarshal is exported by gin/json package.
|
||||
Unmarshal = json.Unmarshal
|
||||
// MarshalIndent is exported by gin/json package.
|
||||
MarshalIndent = json.MarshalIndent
|
||||
// NewDecoder is exported by gin/json package.
|
||||
NewDecoder = json.NewDecoder
|
||||
// NewEncoder is exported by gin/json package.
|
||||
NewEncoder = json.NewEncoder
|
||||
)
|
|
@ -1,27 +0,0 @@
|
|||
// Copyright 2022 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build sonic && avx && (linux || windows || darwin) && amd64
|
||||
// +build sonic
|
||||
// +build avx
|
||||
// +build linux windows darwin
|
||||
// +build amd64
|
||||
|
||||
package json
|
||||
|
||||
import "github.com/bytedance/sonic"
|
||||
|
||||
var (
|
||||
json = sonic.ConfigStd
|
||||
// Marshal is exported by gin/json package.
|
||||
Marshal = json.Marshal
|
||||
// Unmarshal is exported by gin/json package.
|
||||
Unmarshal = json.Unmarshal
|
||||
// MarshalIndent is exported by gin/json package.
|
||||
MarshalIndent = json.MarshalIndent
|
||||
// NewDecoder is exported by gin/json package.
|
||||
NewDecoder = json.NewDecoder
|
||||
// NewEncoder is exported by gin/json package.
|
||||
NewEncoder = json.NewEncoder
|
||||
)
|
277
logger.go
277
logger.go
|
@ -1,270 +1,63 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/mattn/go-isatty"
|
||||
)
|
||||
|
||||
type consoleColorModeValue int
|
||||
|
||||
const (
|
||||
autoColor consoleColorModeValue = iota
|
||||
disableColor
|
||||
forceColor
|
||||
)
|
||||
|
||||
const (
|
||||
green = "\033[97;42m"
|
||||
white = "\033[90;47m"
|
||||
yellow = "\033[90;43m"
|
||||
red = "\033[97;41m"
|
||||
blue = "\033[97;44m"
|
||||
magenta = "\033[97;45m"
|
||||
cyan = "\033[97;46m"
|
||||
reset = "\033[0m"
|
||||
)
|
||||
|
||||
var consoleColorMode = autoColor
|
||||
|
||||
// LoggerConfig defines the config for Logger middleware.
|
||||
type LoggerConfig struct {
|
||||
// Optional. Default value is gin.defaultLogFormatter
|
||||
Formatter LogFormatter
|
||||
|
||||
// Output is a writer where logs are written.
|
||||
// Optional. Default value is gin.DefaultWriter.
|
||||
Output io.Writer
|
||||
|
||||
// SkipPaths is an url path array which logs are not written.
|
||||
// Optional.
|
||||
SkipPaths []string
|
||||
}
|
||||
|
||||
// LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter
|
||||
type LogFormatter func(params LogFormatterParams) string
|
||||
|
||||
// LogFormatterParams is the structure any formatter will be handed when time to log comes
|
||||
type LogFormatterParams struct {
|
||||
Request *http.Request
|
||||
|
||||
// TimeStamp shows the time after the server returns a response.
|
||||
TimeStamp time.Time
|
||||
// StatusCode is HTTP response code.
|
||||
StatusCode int
|
||||
// Latency is how much time the server cost to process a certain request.
|
||||
Latency time.Duration
|
||||
// ClientIP equals Context's ClientIP method.
|
||||
ClientIP string
|
||||
// Method is the HTTP method given to the request.
|
||||
Method string
|
||||
// Path is a path the client requests.
|
||||
Path string
|
||||
// ErrorMessage is set if error has occurred in processing the request.
|
||||
ErrorMessage string
|
||||
// isTerm shows whether gin's output descriptor refers to a terminal.
|
||||
isTerm bool
|
||||
// BodySize is the size of the Response Body
|
||||
BodySize int
|
||||
// Keys are the keys set on the request's context.
|
||||
Keys map[string]any
|
||||
}
|
||||
|
||||
// StatusCodeColor is the ANSI color for appropriately logging http status code to a terminal.
|
||||
func (p *LogFormatterParams) StatusCodeColor() string {
|
||||
code := p.StatusCode
|
||||
|
||||
switch {
|
||||
case code >= http.StatusOK && code < http.StatusMultipleChoices:
|
||||
return green
|
||||
case code >= http.StatusMultipleChoices && code < http.StatusBadRequest:
|
||||
return white
|
||||
case code >= http.StatusBadRequest && code < http.StatusInternalServerError:
|
||||
return yellow
|
||||
default:
|
||||
return red
|
||||
}
|
||||
}
|
||||
|
||||
// MethodColor is the ANSI color for appropriately logging http method to a terminal.
|
||||
func (p *LogFormatterParams) MethodColor() string {
|
||||
method := p.Method
|
||||
|
||||
switch method {
|
||||
case http.MethodGet:
|
||||
return blue
|
||||
case http.MethodPost:
|
||||
return cyan
|
||||
case http.MethodPut:
|
||||
return yellow
|
||||
case http.MethodDelete:
|
||||
return red
|
||||
case http.MethodPatch:
|
||||
return green
|
||||
case http.MethodHead:
|
||||
return magenta
|
||||
case http.MethodOptions:
|
||||
return white
|
||||
default:
|
||||
return reset
|
||||
}
|
||||
}
|
||||
|
||||
// ResetColor resets all escape attributes.
|
||||
func (p *LogFormatterParams) ResetColor() string {
|
||||
return reset
|
||||
}
|
||||
|
||||
// IsOutputColor indicates whether can colors be outputted to the log.
|
||||
func (p *LogFormatterParams) IsOutputColor() bool {
|
||||
return consoleColorMode == forceColor || (consoleColorMode == autoColor && p.isTerm)
|
||||
}
|
||||
|
||||
// defaultLogFormatter is the default log format function Logger middleware uses.
|
||||
var defaultLogFormatter = func(param LogFormatterParams) string {
|
||||
var statusColor, methodColor, resetColor string
|
||||
if param.IsOutputColor() {
|
||||
statusColor = param.StatusCodeColor()
|
||||
methodColor = param.MethodColor()
|
||||
resetColor = param.ResetColor()
|
||||
}
|
||||
|
||||
if param.Latency > time.Minute {
|
||||
param.Latency = param.Latency.Truncate(time.Second)
|
||||
}
|
||||
return fmt.Sprintf("[GIN] %v |%s %3d %s| %13v | %15s |%s %-7s %s %#v\n%s",
|
||||
param.TimeStamp.Format("2006/01/02 - 15:04:05"),
|
||||
statusColor, param.StatusCode, resetColor,
|
||||
param.Latency,
|
||||
param.ClientIP,
|
||||
methodColor, param.Method, resetColor,
|
||||
param.Path,
|
||||
param.ErrorMessage,
|
||||
)
|
||||
}
|
||||
|
||||
// DisableConsoleColor disables color output in the console.
|
||||
func DisableConsoleColor() {
|
||||
consoleColorMode = disableColor
|
||||
}
|
||||
|
||||
// ForceConsoleColor force color output in the console.
|
||||
func ForceConsoleColor() {
|
||||
consoleColorMode = forceColor
|
||||
}
|
||||
|
||||
// ErrorLogger returns a HandlerFunc for any error type.
|
||||
func ErrorLogger() HandlerFunc {
|
||||
return ErrorLoggerT(ErrorTypeAny)
|
||||
}
|
||||
|
||||
// ErrorLoggerT returns a HandlerFunc for a given error type.
|
||||
func ErrorLoggerT(typ ErrorType) HandlerFunc {
|
||||
return func(c *Context) {
|
||||
c.Next()
|
||||
errors := c.Errors.ByType(typ)
|
||||
if len(errors) > 0 {
|
||||
c.JSON(-1, errors)
|
||||
|
||||
if len(c.Errors) > 0 {
|
||||
// -1 status code = do not change current one
|
||||
c.JSON(-1, c.Errors)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Logger instances a Logger middleware that will write the logs to gin.DefaultWriter.
|
||||
// By default, gin.DefaultWriter = os.Stdout.
|
||||
var (
|
||||
green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
|
||||
white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
|
||||
yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109})
|
||||
red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
|
||||
reset = string([]byte{27, 91, 48, 109})
|
||||
)
|
||||
|
||||
func Logger() HandlerFunc {
|
||||
return LoggerWithConfig(LoggerConfig{})
|
||||
}
|
||||
|
||||
// LoggerWithFormatter instance a Logger middleware with the specified log format function.
|
||||
func LoggerWithFormatter(f LogFormatter) HandlerFunc {
|
||||
return LoggerWithConfig(LoggerConfig{
|
||||
Formatter: f,
|
||||
})
|
||||
}
|
||||
|
||||
// LoggerWithWriter instance a Logger middleware with the specified writer buffer.
|
||||
// Example: os.Stdout, a file opened in write mode, a socket...
|
||||
func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
||||
return LoggerWithConfig(LoggerConfig{
|
||||
Output: out,
|
||||
SkipPaths: notlogged,
|
||||
})
|
||||
}
|
||||
|
||||
// LoggerWithConfig instance a Logger middleware with config.
|
||||
func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
||||
formatter := conf.Formatter
|
||||
if formatter == nil {
|
||||
formatter = defaultLogFormatter
|
||||
}
|
||||
|
||||
out := conf.Output
|
||||
if out == nil {
|
||||
out = DefaultWriter
|
||||
}
|
||||
|
||||
notlogged := conf.SkipPaths
|
||||
|
||||
isTerm := true
|
||||
|
||||
if w, ok := out.(*os.File); !ok || os.Getenv("TERM") == "dumb" ||
|
||||
(!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd())) {
|
||||
isTerm = false
|
||||
}
|
||||
|
||||
var skip map[string]struct{}
|
||||
|
||||
if length := len(notlogged); length > 0 {
|
||||
skip = make(map[string]struct{}, length)
|
||||
|
||||
for _, path := range notlogged {
|
||||
skip[path] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
logger := log.New(os.Stdout, "", 0)
|
||||
return func(c *Context) {
|
||||
// Start timer
|
||||
start := time.Now()
|
||||
path := c.Request.URL.Path
|
||||
raw := c.Request.URL.RawQuery
|
||||
|
||||
// Process request
|
||||
c.Next()
|
||||
|
||||
// Log only when path is not being skipped
|
||||
if _, ok := skip[path]; !ok {
|
||||
param := LogFormatterParams{
|
||||
Request: c.Request,
|
||||
isTerm: isTerm,
|
||||
Keys: c.Keys,
|
||||
var color string
|
||||
code := c.Writer.Status()
|
||||
switch {
|
||||
case code >= 200 && code <= 299:
|
||||
color = green
|
||||
case code >= 300 && code <= 399:
|
||||
color = white
|
||||
case code >= 400 && code <= 499:
|
||||
color = yellow
|
||||
default:
|
||||
color = red
|
||||
}
|
||||
latency := time.Since(start)
|
||||
logger.Printf("[GIN] %v |%s %3d %s| %12v | %3s %s\n",
|
||||
time.Now().Format("2006/01/02 - 15:04:05"),
|
||||
color, c.Writer.Status(), reset,
|
||||
latency,
|
||||
c.Req.Method, c.Req.URL.Path,
|
||||
)
|
||||
|
||||
// Stop timer
|
||||
param.TimeStamp = time.Now()
|
||||
param.Latency = param.TimeStamp.Sub(start)
|
||||
|
||||
param.ClientIP = c.ClientIP()
|
||||
param.Method = c.Request.Method
|
||||
param.StatusCode = c.Writer.Status()
|
||||
param.ErrorMessage = c.Errors.ByType(ErrorTypePrivate).String()
|
||||
|
||||
param.BodySize = c.Writer.Size()
|
||||
|
||||
if raw != "" {
|
||||
path = path + "?" + raw
|
||||
}
|
||||
|
||||
param.Path = path
|
||||
|
||||
fmt.Fprint(out, formatter(param))
|
||||
// Calculate resolution time
|
||||
if len(c.Errors) > 0 {
|
||||
fmt.Println(c.Errors.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
435
logger_test.go
435
logger_test.go
|
@ -1,435 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func init() {
|
||||
SetMode(TestMode)
|
||||
}
|
||||
|
||||
func TestLogger(t *testing.T) {
|
||||
buffer := new(bytes.Buffer)
|
||||
router := New()
|
||||
router.Use(LoggerWithWriter(buffer))
|
||||
router.GET("/example", func(c *Context) {})
|
||||
router.POST("/example", func(c *Context) {})
|
||||
router.PUT("/example", func(c *Context) {})
|
||||
router.DELETE("/example", func(c *Context) {})
|
||||
router.PATCH("/example", func(c *Context) {})
|
||||
router.HEAD("/example", func(c *Context) {})
|
||||
router.OPTIONS("/example", func(c *Context) {})
|
||||
|
||||
PerformRequest(router, "GET", "/example?a=100")
|
||||
assert.Contains(t, buffer.String(), "200")
|
||||
assert.Contains(t, buffer.String(), "GET")
|
||||
assert.Contains(t, buffer.String(), "/example")
|
||||
assert.Contains(t, buffer.String(), "a=100")
|
||||
|
||||
// I wrote these first (extending the above) but then realized they are more
|
||||
// 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")
|
||||
assert.Contains(t, buffer.String(), "200")
|
||||
assert.Contains(t, buffer.String(), "POST")
|
||||
assert.Contains(t, buffer.String(), "/example")
|
||||
|
||||
buffer.Reset()
|
||||
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")
|
||||
assert.Contains(t, buffer.String(), "200")
|
||||
assert.Contains(t, buffer.String(), "DELETE")
|
||||
assert.Contains(t, buffer.String(), "/example")
|
||||
|
||||
buffer.Reset()
|
||||
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")
|
||||
assert.Contains(t, buffer.String(), "200")
|
||||
assert.Contains(t, buffer.String(), "HEAD")
|
||||
assert.Contains(t, buffer.String(), "/example")
|
||||
|
||||
buffer.Reset()
|
||||
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")
|
||||
assert.Contains(t, buffer.String(), "404")
|
||||
assert.Contains(t, buffer.String(), "GET")
|
||||
assert.Contains(t, buffer.String(), "/notfound")
|
||||
}
|
||||
|
||||
func TestLoggerWithConfig(t *testing.T) {
|
||||
buffer := new(bytes.Buffer)
|
||||
router := New()
|
||||
router.Use(LoggerWithConfig(LoggerConfig{Output: buffer}))
|
||||
router.GET("/example", func(c *Context) {})
|
||||
router.POST("/example", func(c *Context) {})
|
||||
router.PUT("/example", func(c *Context) {})
|
||||
router.DELETE("/example", func(c *Context) {})
|
||||
router.PATCH("/example", func(c *Context) {})
|
||||
router.HEAD("/example", func(c *Context) {})
|
||||
router.OPTIONS("/example", func(c *Context) {})
|
||||
|
||||
PerformRequest(router, "GET", "/example?a=100")
|
||||
assert.Contains(t, buffer.String(), "200")
|
||||
assert.Contains(t, buffer.String(), "GET")
|
||||
assert.Contains(t, buffer.String(), "/example")
|
||||
assert.Contains(t, buffer.String(), "a=100")
|
||||
|
||||
// I wrote these first (extending the above) but then realized they are more
|
||||
// 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")
|
||||
assert.Contains(t, buffer.String(), "200")
|
||||
assert.Contains(t, buffer.String(), "POST")
|
||||
assert.Contains(t, buffer.String(), "/example")
|
||||
|
||||
buffer.Reset()
|
||||
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")
|
||||
assert.Contains(t, buffer.String(), "200")
|
||||
assert.Contains(t, buffer.String(), "DELETE")
|
||||
assert.Contains(t, buffer.String(), "/example")
|
||||
|
||||
buffer.Reset()
|
||||
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")
|
||||
assert.Contains(t, buffer.String(), "200")
|
||||
assert.Contains(t, buffer.String(), "HEAD")
|
||||
assert.Contains(t, buffer.String(), "/example")
|
||||
|
||||
buffer.Reset()
|
||||
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")
|
||||
assert.Contains(t, buffer.String(), "404")
|
||||
assert.Contains(t, buffer.String(), "GET")
|
||||
assert.Contains(t, buffer.String(), "/notfound")
|
||||
}
|
||||
|
||||
func TestLoggerWithFormatter(t *testing.T) {
|
||||
buffer := new(bytes.Buffer)
|
||||
|
||||
d := DefaultWriter
|
||||
DefaultWriter = buffer
|
||||
defer func() {
|
||||
DefaultWriter = d
|
||||
}()
|
||||
|
||||
router := New()
|
||||
router.Use(LoggerWithFormatter(func(param LogFormatterParams) string {
|
||||
return fmt.Sprintf("[FORMATTER TEST] %v | %3d | %13v | %15s | %-7s %#v\n%s",
|
||||
param.TimeStamp.Format("2006/01/02 - 15:04:05"),
|
||||
param.StatusCode,
|
||||
param.Latency,
|
||||
param.ClientIP,
|
||||
param.Method,
|
||||
param.Path,
|
||||
param.ErrorMessage,
|
||||
)
|
||||
}))
|
||||
router.GET("/example", func(c *Context) {})
|
||||
PerformRequest(router, "GET", "/example?a=100")
|
||||
|
||||
// output test
|
||||
assert.Contains(t, buffer.String(), "[FORMATTER TEST]")
|
||||
assert.Contains(t, buffer.String(), "200")
|
||||
assert.Contains(t, buffer.String(), "GET")
|
||||
assert.Contains(t, buffer.String(), "/example")
|
||||
assert.Contains(t, buffer.String(), "a=100")
|
||||
}
|
||||
|
||||
func TestLoggerWithConfigFormatting(t *testing.T) {
|
||||
var gotParam LogFormatterParams
|
||||
var gotKeys map[string]any
|
||||
buffer := new(bytes.Buffer)
|
||||
|
||||
router := New()
|
||||
router.engine.trustedCIDRs, _ = router.engine.prepareTrustedCIDRs()
|
||||
|
||||
router.Use(LoggerWithConfig(LoggerConfig{
|
||||
Output: buffer,
|
||||
Formatter: func(param LogFormatterParams) string {
|
||||
// for assert test
|
||||
gotParam = param
|
||||
|
||||
return fmt.Sprintf("[FORMATTER TEST] %v | %3d | %13v | %15s | %-7s %s\n%s",
|
||||
param.TimeStamp.Format("2006/01/02 - 15:04:05"),
|
||||
param.StatusCode,
|
||||
param.Latency,
|
||||
param.ClientIP,
|
||||
param.Method,
|
||||
param.Path,
|
||||
param.ErrorMessage,
|
||||
)
|
||||
},
|
||||
}))
|
||||
router.GET("/example", func(c *Context) {
|
||||
// set dummy ClientIP
|
||||
c.Request.Header.Set("X-Forwarded-For", "20.20.20.20")
|
||||
gotKeys = c.Keys
|
||||
time.Sleep(time.Millisecond)
|
||||
})
|
||||
PerformRequest(router, "GET", "/example?a=100")
|
||||
|
||||
// output test
|
||||
assert.Contains(t, buffer.String(), "[FORMATTER TEST]")
|
||||
assert.Contains(t, buffer.String(), "200")
|
||||
assert.Contains(t, buffer.String(), "GET")
|
||||
assert.Contains(t, buffer.String(), "/example")
|
||||
assert.Contains(t, buffer.String(), "a=100")
|
||||
|
||||
// LogFormatterParams test
|
||||
assert.NotNil(t, gotParam.Request)
|
||||
assert.NotEmpty(t, gotParam.TimeStamp)
|
||||
assert.Equal(t, 200, gotParam.StatusCode)
|
||||
assert.NotEmpty(t, gotParam.Latency)
|
||||
assert.Equal(t, "20.20.20.20", gotParam.ClientIP)
|
||||
assert.Equal(t, "GET", gotParam.Method)
|
||||
assert.Equal(t, "/example?a=100", gotParam.Path)
|
||||
assert.Empty(t, gotParam.ErrorMessage)
|
||||
assert.Equal(t, gotKeys, gotParam.Keys)
|
||||
}
|
||||
|
||||
func TestDefaultLogFormatter(t *testing.T) {
|
||||
timeStamp := time.Unix(1544173902, 0).UTC()
|
||||
|
||||
termFalseParam := LogFormatterParams{
|
||||
TimeStamp: timeStamp,
|
||||
StatusCode: 200,
|
||||
Latency: time.Second * 5,
|
||||
ClientIP: "20.20.20.20",
|
||||
Method: "GET",
|
||||
Path: "/",
|
||||
ErrorMessage: "",
|
||||
isTerm: false,
|
||||
}
|
||||
|
||||
termTrueParam := LogFormatterParams{
|
||||
TimeStamp: timeStamp,
|
||||
StatusCode: 200,
|
||||
Latency: time.Second * 5,
|
||||
ClientIP: "20.20.20.20",
|
||||
Method: "GET",
|
||||
Path: "/",
|
||||
ErrorMessage: "",
|
||||
isTerm: true,
|
||||
}
|
||||
termTrueLongDurationParam := LogFormatterParams{
|
||||
TimeStamp: timeStamp,
|
||||
StatusCode: 200,
|
||||
Latency: time.Millisecond * 9876543210,
|
||||
ClientIP: "20.20.20.20",
|
||||
Method: "GET",
|
||||
Path: "/",
|
||||
ErrorMessage: "",
|
||||
isTerm: true,
|
||||
}
|
||||
|
||||
termFalseLongDurationParam := LogFormatterParams{
|
||||
TimeStamp: timeStamp,
|
||||
StatusCode: 200,
|
||||
Latency: time.Millisecond * 9876543210,
|
||||
ClientIP: "20.20.20.20",
|
||||
Method: "GET",
|
||||
Path: "/",
|
||||
ErrorMessage: "",
|
||||
isTerm: false,
|
||||
}
|
||||
|
||||
assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 200 | 5s | 20.20.20.20 | GET \"/\"\n", defaultLogFormatter(termFalseParam))
|
||||
assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 200 | 2743h29m3s | 20.20.20.20 | GET \"/\"\n", defaultLogFormatter(termFalseLongDurationParam))
|
||||
|
||||
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) {
|
||||
colorForMethod := func(method string) string {
|
||||
p := LogFormatterParams{
|
||||
Method: method,
|
||||
}
|
||||
return p.MethodColor()
|
||||
}
|
||||
|
||||
assert.Equal(t, blue, colorForMethod("GET"), "get should be blue")
|
||||
assert.Equal(t, cyan, colorForMethod("POST"), "post should be cyan")
|
||||
assert.Equal(t, yellow, colorForMethod("PUT"), "put should be yellow")
|
||||
assert.Equal(t, red, colorForMethod("DELETE"), "delete should be red")
|
||||
assert.Equal(t, green, colorForMethod("PATCH"), "patch should be green")
|
||||
assert.Equal(t, magenta, colorForMethod("HEAD"), "head should be magenta")
|
||||
assert.Equal(t, white, colorForMethod("OPTIONS"), "options should be white")
|
||||
assert.Equal(t, reset, colorForMethod("TRACE"), "trace is not defined and should be the reset color")
|
||||
}
|
||||
|
||||
func TestColorForStatus(t *testing.T) {
|
||||
colorForStatus := func(code int) string {
|
||||
p := LogFormatterParams{
|
||||
StatusCode: code,
|
||||
}
|
||||
return p.StatusCodeColor()
|
||||
}
|
||||
|
||||
assert.Equal(t, green, colorForStatus(http.StatusOK), "2xx should be green")
|
||||
assert.Equal(t, white, colorForStatus(http.StatusMovedPermanently), "3xx should be white")
|
||||
assert.Equal(t, yellow, colorForStatus(http.StatusNotFound), "4xx should be yellow")
|
||||
assert.Equal(t, red, colorForStatus(2), "other things should be red")
|
||||
}
|
||||
|
||||
func TestResetColor(t *testing.T) {
|
||||
p := LogFormatterParams{}
|
||||
assert.Equal(t, string([]byte{27, 91, 48, 109}), p.ResetColor())
|
||||
}
|
||||
|
||||
func TestIsOutputColor(t *testing.T) {
|
||||
// test with isTerm flag true.
|
||||
p := LogFormatterParams{
|
||||
isTerm: true,
|
||||
}
|
||||
|
||||
consoleColorMode = autoColor
|
||||
assert.Equal(t, true, p.IsOutputColor())
|
||||
|
||||
ForceConsoleColor()
|
||||
assert.Equal(t, true, p.IsOutputColor())
|
||||
|
||||
DisableConsoleColor()
|
||||
assert.Equal(t, false, p.IsOutputColor())
|
||||
|
||||
// test with isTerm flag false.
|
||||
p = LogFormatterParams{
|
||||
isTerm: false,
|
||||
}
|
||||
|
||||
consoleColorMode = autoColor
|
||||
assert.Equal(t, false, p.IsOutputColor())
|
||||
|
||||
ForceConsoleColor()
|
||||
assert.Equal(t, true, p.IsOutputColor())
|
||||
|
||||
DisableConsoleColor()
|
||||
assert.Equal(t, false, p.IsOutputColor())
|
||||
|
||||
// reset console color mode.
|
||||
consoleColorMode = autoColor
|
||||
}
|
||||
|
||||
func TestErrorLogger(t *testing.T) {
|
||||
router := New()
|
||||
router.Use(ErrorLogger())
|
||||
router.GET("/error", func(c *Context) {
|
||||
c.Error(errors.New("this is an error")) //nolint: errcheck
|
||||
})
|
||||
router.GET("/abort", func(c *Context) {
|
||||
c.AbortWithError(http.StatusUnauthorized, errors.New("no authorized")) //nolint: errcheck
|
||||
})
|
||||
router.GET("/print", func(c *Context) {
|
||||
c.Error(errors.New("this is an error")) //nolint: errcheck
|
||||
c.String(http.StatusInternalServerError, "hola!")
|
||||
})
|
||||
|
||||
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")
|
||||
assert.Equal(t, http.StatusUnauthorized, w.Code)
|
||||
assert.Equal(t, "{\"error\":\"no authorized\"}", w.Body.String())
|
||||
|
||||
w = PerformRequest(router, "GET", "/print")
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Equal(t, "hola!{\"error\":\"this is an error\"}", w.Body.String())
|
||||
}
|
||||
|
||||
func TestLoggerWithWriterSkippingPaths(t *testing.T) {
|
||||
buffer := new(bytes.Buffer)
|
||||
router := New()
|
||||
router.Use(LoggerWithWriter(buffer, "/skipped"))
|
||||
router.GET("/logged", func(c *Context) {})
|
||||
router.GET("/skipped", func(c *Context) {})
|
||||
|
||||
PerformRequest(router, "GET", "/logged")
|
||||
assert.Contains(t, buffer.String(), "200")
|
||||
|
||||
buffer.Reset()
|
||||
PerformRequest(router, "GET", "/skipped")
|
||||
assert.Contains(t, buffer.String(), "")
|
||||
}
|
||||
|
||||
func TestLoggerWithConfigSkippingPaths(t *testing.T) {
|
||||
buffer := new(bytes.Buffer)
|
||||
router := New()
|
||||
router.Use(LoggerWithConfig(LoggerConfig{
|
||||
Output: buffer,
|
||||
SkipPaths: []string{"/skipped"},
|
||||
}))
|
||||
router.GET("/logged", func(c *Context) {})
|
||||
router.GET("/skipped", func(c *Context) {})
|
||||
|
||||
PerformRequest(router, "GET", "/logged")
|
||||
assert.Contains(t, buffer.String(), "200")
|
||||
|
||||
buffer.Reset()
|
||||
PerformRequest(router, "GET", "/skipped")
|
||||
assert.Contains(t, buffer.String(), "")
|
||||
}
|
||||
|
||||
func TestDisableConsoleColor(t *testing.T) {
|
||||
New()
|
||||
assert.Equal(t, autoColor, consoleColorMode)
|
||||
DisableConsoleColor()
|
||||
assert.Equal(t, disableColor, consoleColorMode)
|
||||
|
||||
// reset console color mode.
|
||||
consoleColorMode = autoColor
|
||||
}
|
||||
|
||||
func TestForceConsoleColor(t *testing.T) {
|
||||
New()
|
||||
assert.Equal(t, autoColor, consoleColorMode)
|
||||
ForceConsoleColor()
|
||||
assert.Equal(t, forceColor, consoleColorMode)
|
||||
|
||||
// reset console color mode.
|
||||
consoleColorMode = autoColor
|
||||
}
|
|
@ -1,253 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-contrib/sse"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMiddlewareGeneralCase(t *testing.T) {
|
||||
signature := ""
|
||||
router := New()
|
||||
router.Use(func(c *Context) {
|
||||
signature += "A"
|
||||
c.Next()
|
||||
signature += "B"
|
||||
})
|
||||
router.Use(func(c *Context) {
|
||||
signature += "C"
|
||||
})
|
||||
router.GET("/", func(c *Context) {
|
||||
signature += "D"
|
||||
})
|
||||
router.NoRoute(func(c *Context) {
|
||||
signature += " X "
|
||||
})
|
||||
router.NoMethod(func(c *Context) {
|
||||
signature += " XX "
|
||||
})
|
||||
// RUN
|
||||
w := PerformRequest(router, "GET", "/")
|
||||
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Equal(t, "ACDB", signature)
|
||||
}
|
||||
|
||||
func TestMiddlewareNoRoute(t *testing.T) {
|
||||
signature := ""
|
||||
router := New()
|
||||
router.Use(func(c *Context) {
|
||||
signature += "A"
|
||||
c.Next()
|
||||
signature += "B"
|
||||
})
|
||||
router.Use(func(c *Context) {
|
||||
signature += "C"
|
||||
c.Next()
|
||||
c.Next()
|
||||
c.Next()
|
||||
c.Next()
|
||||
signature += "D"
|
||||
})
|
||||
router.NoRoute(func(c *Context) {
|
||||
signature += "E"
|
||||
c.Next()
|
||||
signature += "F"
|
||||
}, func(c *Context) {
|
||||
signature += "G"
|
||||
c.Next()
|
||||
signature += "H"
|
||||
})
|
||||
router.NoMethod(func(c *Context) {
|
||||
signature += " X "
|
||||
})
|
||||
// RUN
|
||||
w := PerformRequest(router, "GET", "/")
|
||||
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
assert.Equal(t, "ACEGHFDB", signature)
|
||||
}
|
||||
|
||||
func TestMiddlewareNoMethodEnabled(t *testing.T) {
|
||||
signature := ""
|
||||
router := New()
|
||||
router.HandleMethodNotAllowed = true
|
||||
router.Use(func(c *Context) {
|
||||
signature += "A"
|
||||
c.Next()
|
||||
signature += "B"
|
||||
})
|
||||
router.Use(func(c *Context) {
|
||||
signature += "C"
|
||||
c.Next()
|
||||
signature += "D"
|
||||
})
|
||||
router.NoMethod(func(c *Context) {
|
||||
signature += "E"
|
||||
c.Next()
|
||||
signature += "F"
|
||||
}, func(c *Context) {
|
||||
signature += "G"
|
||||
c.Next()
|
||||
signature += "H"
|
||||
})
|
||||
router.NoRoute(func(c *Context) {
|
||||
signature += " X "
|
||||
})
|
||||
router.POST("/", func(c *Context) {
|
||||
signature += " XX "
|
||||
})
|
||||
// RUN
|
||||
w := PerformRequest(router, "GET", "/")
|
||||
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
||||
assert.Equal(t, "ACEGHFDB", signature)
|
||||
}
|
||||
|
||||
func TestMiddlewareNoMethodDisabled(t *testing.T) {
|
||||
signature := ""
|
||||
router := New()
|
||||
|
||||
// NoMethod disabled
|
||||
router.HandleMethodNotAllowed = false
|
||||
|
||||
router.Use(func(c *Context) {
|
||||
signature += "A"
|
||||
c.Next()
|
||||
signature += "B"
|
||||
})
|
||||
router.Use(func(c *Context) {
|
||||
signature += "C"
|
||||
c.Next()
|
||||
signature += "D"
|
||||
})
|
||||
router.NoMethod(func(c *Context) {
|
||||
signature += "E"
|
||||
c.Next()
|
||||
signature += "F"
|
||||
}, func(c *Context) {
|
||||
signature += "G"
|
||||
c.Next()
|
||||
signature += "H"
|
||||
})
|
||||
router.NoRoute(func(c *Context) {
|
||||
signature += " X "
|
||||
})
|
||||
router.POST("/", func(c *Context) {
|
||||
signature += " XX "
|
||||
})
|
||||
|
||||
// RUN
|
||||
w := PerformRequest(router, "GET", "/")
|
||||
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
assert.Equal(t, "AC X DB", signature)
|
||||
}
|
||||
|
||||
func TestMiddlewareAbort(t *testing.T) {
|
||||
signature := ""
|
||||
router := New()
|
||||
router.Use(func(c *Context) {
|
||||
signature += "A"
|
||||
})
|
||||
router.Use(func(c *Context) {
|
||||
signature += "C"
|
||||
c.AbortWithStatus(http.StatusUnauthorized)
|
||||
c.Next()
|
||||
signature += "D"
|
||||
})
|
||||
router.GET("/", func(c *Context) {
|
||||
signature += " X "
|
||||
c.Next()
|
||||
signature += " XX "
|
||||
})
|
||||
|
||||
// RUN
|
||||
w := PerformRequest(router, "GET", "/")
|
||||
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusUnauthorized, w.Code)
|
||||
assert.Equal(t, "ACD", signature)
|
||||
}
|
||||
|
||||
func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
|
||||
signature := ""
|
||||
router := New()
|
||||
router.Use(func(c *Context) {
|
||||
signature += "A"
|
||||
c.Next()
|
||||
c.AbortWithStatus(http.StatusGone)
|
||||
signature += "B"
|
||||
})
|
||||
router.GET("/", func(c *Context) {
|
||||
signature += "C"
|
||||
c.Next()
|
||||
})
|
||||
// RUN
|
||||
w := PerformRequest(router, "GET", "/")
|
||||
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusGone, w.Code)
|
||||
assert.Equal(t, "ACB", signature)
|
||||
}
|
||||
|
||||
// TestFailHandlersChain - ensure that Fail interrupt used middleware in fifo order as
|
||||
// as well as Abort
|
||||
func TestMiddlewareFailHandlersChain(t *testing.T) {
|
||||
// SETUP
|
||||
signature := ""
|
||||
router := New()
|
||||
router.Use(func(context *Context) {
|
||||
signature += "A"
|
||||
context.AbortWithError(http.StatusInternalServerError, errors.New("foo")) //nolint: errcheck
|
||||
})
|
||||
router.Use(func(context *Context) {
|
||||
signature += "B"
|
||||
context.Next()
|
||||
signature += "C"
|
||||
})
|
||||
// RUN
|
||||
w := PerformRequest(router, "GET", "/")
|
||||
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Equal(t, "A", signature)
|
||||
}
|
||||
|
||||
func TestMiddlewareWrite(t *testing.T) {
|
||||
router := New()
|
||||
router.Use(func(c *Context) {
|
||||
c.String(http.StatusBadRequest, "hola\n")
|
||||
})
|
||||
router.Use(func(c *Context) {
|
||||
c.XML(http.StatusBadRequest, H{"foo": "bar"})
|
||||
})
|
||||
router.Use(func(c *Context) {
|
||||
c.JSON(http.StatusBadRequest, H{"foo": "bar"})
|
||||
})
|
||||
router.GET("/", func(c *Context) {
|
||||
c.JSON(http.StatusBadRequest, H{"foo": "bar"})
|
||||
}, func(c *Context) {
|
||||
c.Render(http.StatusBadRequest, sse.Event{
|
||||
Event: "test",
|
||||
Data: "message",
|
||||
})
|
||||
})
|
||||
|
||||
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))
|
||||
}
|
100
mode.go
100
mode.go
|
@ -1,100 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"git.internal/re/gin/binding"
|
||||
)
|
||||
|
||||
// EnvGinMode indicates environment name for gin mode.
|
||||
const EnvGinMode = "GIN_MODE"
|
||||
|
||||
const (
|
||||
// DebugMode indicates gin mode is debug.
|
||||
DebugMode = "debug"
|
||||
// ReleaseMode indicates gin mode is release.
|
||||
ReleaseMode = "release"
|
||||
// TestMode indicates gin mode is test.
|
||||
TestMode = "test"
|
||||
)
|
||||
|
||||
const (
|
||||
debugCode = iota
|
||||
releaseCode
|
||||
testCode
|
||||
)
|
||||
|
||||
// DefaultWriter is the default io.Writer used by Gin for debug output and
|
||||
// middleware output like Logger() or Recovery().
|
||||
// Note that both Logger and Recovery provides custom ways to configure their
|
||||
// output io.Writer.
|
||||
// To support coloring in Windows use:
|
||||
//
|
||||
// import "github.com/mattn/go-colorable"
|
||||
// gin.DefaultWriter = colorable.NewColorableStdout()
|
||||
var DefaultWriter io.Writer = os.Stdout
|
||||
|
||||
// DefaultErrorWriter is the default io.Writer used by Gin to debug errors
|
||||
var DefaultErrorWriter io.Writer = os.Stderr
|
||||
|
||||
var (
|
||||
ginMode = debugCode
|
||||
modeName = DebugMode
|
||||
)
|
||||
|
||||
func init() {
|
||||
mode := os.Getenv(EnvGinMode)
|
||||
SetMode(mode)
|
||||
}
|
||||
|
||||
// SetMode sets gin mode according to input string.
|
||||
func SetMode(value string) {
|
||||
if value == "" {
|
||||
if flag.Lookup("test.v") != nil {
|
||||
value = TestMode
|
||||
} else {
|
||||
value = DebugMode
|
||||
}
|
||||
}
|
||||
|
||||
switch value {
|
||||
case DebugMode:
|
||||
ginMode = debugCode
|
||||
case ReleaseMode:
|
||||
ginMode = releaseCode
|
||||
case TestMode:
|
||||
ginMode = testCode
|
||||
default:
|
||||
panic("gin mode unknown: " + value + " (available mode: debug release test)")
|
||||
}
|
||||
|
||||
modeName = value
|
||||
}
|
||||
|
||||
// DisableBindValidation closes the default validator.
|
||||
func DisableBindValidation() {
|
||||
binding.Validator = nil
|
||||
}
|
||||
|
||||
// EnableJsonDecoderUseNumber sets true for binding.EnableDecoderUseNumber to
|
||||
// call the UseNumber method on the JSON Decoder instance.
|
||||
func EnableJsonDecoderUseNumber() {
|
||||
binding.EnableDecoderUseNumber = true
|
||||
}
|
||||
|
||||
// EnableJsonDecoderDisallowUnknownFields sets true for binding.EnableDecoderDisallowUnknownFields to
|
||||
// call the DisallowUnknownFields method on the JSON Decoder instance.
|
||||
func EnableJsonDecoderDisallowUnknownFields() {
|
||||
binding.EnableDecoderDisallowUnknownFields = true
|
||||
}
|
||||
|
||||
// Mode returns current gin mode.
|
||||
func Mode() string {
|
||||
return modeName
|
||||
}
|
69
mode_test.go
69
mode_test.go
|
@ -1,69 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"git.internal/re/gin/binding"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func init() {
|
||||
os.Setenv(EnvGinMode, TestMode)
|
||||
}
|
||||
|
||||
func TestSetMode(t *testing.T) {
|
||||
assert.Equal(t, testCode, ginMode)
|
||||
assert.Equal(t, TestMode, Mode())
|
||||
os.Unsetenv(EnvGinMode)
|
||||
|
||||
SetMode("")
|
||||
assert.Equal(t, testCode, ginMode)
|
||||
assert.Equal(t, TestMode, Mode())
|
||||
|
||||
tmp := flag.CommandLine
|
||||
flag.CommandLine = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
SetMode("")
|
||||
assert.Equal(t, debugCode, ginMode)
|
||||
assert.Equal(t, DebugMode, Mode())
|
||||
flag.CommandLine = tmp
|
||||
|
||||
SetMode(DebugMode)
|
||||
assert.Equal(t, debugCode, ginMode)
|
||||
assert.Equal(t, DebugMode, Mode())
|
||||
|
||||
SetMode(ReleaseMode)
|
||||
assert.Equal(t, releaseCode, ginMode)
|
||||
assert.Equal(t, ReleaseMode, Mode())
|
||||
|
||||
SetMode(TestMode)
|
||||
assert.Equal(t, testCode, ginMode)
|
||||
assert.Equal(t, TestMode, Mode())
|
||||
|
||||
assert.Panics(t, func() { SetMode("unknown") })
|
||||
}
|
||||
|
||||
func TestDisableBindValidation(t *testing.T) {
|
||||
v := binding.Validator
|
||||
assert.NotNil(t, binding.Validator)
|
||||
DisableBindValidation()
|
||||
assert.Nil(t, binding.Validator)
|
||||
binding.Validator = v
|
||||
}
|
||||
|
||||
func TestEnableJsonDecoderUseNumber(t *testing.T) {
|
||||
assert.False(t, binding.EnableDecoderUseNumber)
|
||||
EnableJsonDecoderUseNumber()
|
||||
assert.True(t, binding.EnableDecoderUseNumber)
|
||||
}
|
||||
|
||||
func TestEnableJsonDecoderDisallowUnknownFields(t *testing.T) {
|
||||
assert.False(t, binding.EnableDecoderDisallowUnknownFields)
|
||||
EnableJsonDecoderDisallowUnknownFields()
|
||||
assert.True(t, binding.EnableDecoderDisallowUnknownFields)
|
||||
}
|
150
path.go
150
path.go
|
@ -1,150 +0,0 @@
|
|||
// Copyright 2013 Julien Schmidt. All rights reserved.
|
||||
// Based on the path package, Copyright 2009 The Go Authors.
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// at https://github.com/julienschmidt/httprouter/blob/master/LICENSE.
|
||||
|
||||
package gin
|
||||
|
||||
// cleanPath is the URL version of path.Clean, it returns a canonical URL path
|
||||
// for p, eliminating . and .. elements.
|
||||
//
|
||||
// The following rules are applied iteratively until no further processing can
|
||||
// be done:
|
||||
// 1. Replace multiple slashes with a single slash.
|
||||
// 2. Eliminate each . path name element (the current directory).
|
||||
// 3. Eliminate each inner .. path name element (the parent directory)
|
||||
// along with the non-.. element that precedes it.
|
||||
// 4. Eliminate .. elements that begin a rooted path:
|
||||
// that is, replace "/.." by "/" at the beginning of a path.
|
||||
//
|
||||
// If the result of this process is an empty string, "/" is returned.
|
||||
func cleanPath(p string) string {
|
||||
const stackBufSize = 128
|
||||
// Turn empty string into "/"
|
||||
if p == "" {
|
||||
return "/"
|
||||
}
|
||||
|
||||
// Reasonably sized buffer on stack to avoid allocations in the common case.
|
||||
// If a larger buffer is required, it gets allocated dynamically.
|
||||
buf := make([]byte, 0, stackBufSize)
|
||||
|
||||
n := len(p)
|
||||
|
||||
// Invariants:
|
||||
// reading from path; r is index of next byte to process.
|
||||
// writing to buf; w is index of next byte to write.
|
||||
|
||||
// path must start with '/'
|
||||
r := 1
|
||||
w := 1
|
||||
|
||||
if p[0] != '/' {
|
||||
r = 0
|
||||
|
||||
if n+1 > stackBufSize {
|
||||
buf = make([]byte, n+1)
|
||||
} else {
|
||||
buf = buf[:n+1]
|
||||
}
|
||||
buf[0] = '/'
|
||||
}
|
||||
|
||||
trailing := n > 1 && p[n-1] == '/'
|
||||
|
||||
// A bit more clunky without a 'lazybuf' like the path package, but the loop
|
||||
// gets completely inlined (bufApp calls).
|
||||
// loop has no expensive function calls (except 1x make) // So in contrast to the path package this loop has no expensive function
|
||||
// calls (except make, if needed).
|
||||
|
||||
for r < n {
|
||||
switch {
|
||||
case p[r] == '/':
|
||||
// empty path element, trailing slash is added after the end
|
||||
r++
|
||||
|
||||
case p[r] == '.' && r+1 == n:
|
||||
trailing = true
|
||||
r++
|
||||
|
||||
case p[r] == '.' && p[r+1] == '/':
|
||||
// . element
|
||||
r += 2
|
||||
|
||||
case p[r] == '.' && p[r+1] == '.' && (r+2 == n || p[r+2] == '/'):
|
||||
// .. element: remove to last /
|
||||
r += 3
|
||||
|
||||
if w > 1 {
|
||||
// can backtrack
|
||||
w--
|
||||
|
||||
if len(buf) == 0 {
|
||||
for w > 1 && p[w] != '/' {
|
||||
w--
|
||||
}
|
||||
} else {
|
||||
for w > 1 && buf[w] != '/' {
|
||||
w--
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
// Real path element.
|
||||
// Add slash if needed
|
||||
if w > 1 {
|
||||
bufApp(&buf, p, w, '/')
|
||||
w++
|
||||
}
|
||||
|
||||
// Copy element
|
||||
for r < n && p[r] != '/' {
|
||||
bufApp(&buf, p, w, p[r])
|
||||
w++
|
||||
r++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Re-append trailing slash
|
||||
if trailing && w > 1 {
|
||||
bufApp(&buf, p, w, '/')
|
||||
w++
|
||||
}
|
||||
|
||||
// If the original string was not modified (or only shortened at the end),
|
||||
// return the respective substring of the original string.
|
||||
// Otherwise return a new string from the buffer.
|
||||
if len(buf) == 0 {
|
||||
return p[:w]
|
||||
}
|
||||
return string(buf[:w])
|
||||
}
|
||||
|
||||
// Internal helper to lazily create a buffer if necessary.
|
||||
// Calls to this function get inlined.
|
||||
func bufApp(buf *[]byte, s string, w int, c byte) {
|
||||
b := *buf
|
||||
if len(b) == 0 {
|
||||
// No modification of the original string so far.
|
||||
// If the next character is the same as in the original string, we do
|
||||
// not yet have to allocate a buffer.
|
||||
if s[w] == c {
|
||||
return
|
||||
}
|
||||
|
||||
// Otherwise use either the stack buffer, if it is large enough, or
|
||||
// allocate a new buffer on the heap, and copy all previous characters.
|
||||
length := len(s)
|
||||
if length > cap(b) {
|
||||
*buf = make([]byte, length)
|
||||
} else {
|
||||
*buf = (*buf)[:length]
|
||||
}
|
||||
b = *buf
|
||||
|
||||
copy(b, s[:w])
|
||||
}
|
||||
b[w] = c
|
||||
}
|
140
path_test.go
140
path_test.go
|
@ -1,140 +0,0 @@
|
|||
// Copyright 2013 Julien Schmidt. All rights reserved.
|
||||
// Based on the path package, Copyright 2009 The Go Authors.
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// at https://github.com/julienschmidt/httprouter/blob/master/LICENSE
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type cleanPathTest struct {
|
||||
path, result string
|
||||
}
|
||||
|
||||
var cleanTests = []cleanPathTest{
|
||||
// Already clean
|
||||
{"/", "/"},
|
||||
{"/abc", "/abc"},
|
||||
{"/a/b/c", "/a/b/c"},
|
||||
{"/abc/", "/abc/"},
|
||||
{"/a/b/c/", "/a/b/c/"},
|
||||
|
||||
// missing root
|
||||
{"", "/"},
|
||||
{"a/", "/a/"},
|
||||
{"abc", "/abc"},
|
||||
{"abc/def", "/abc/def"},
|
||||
{"a/b/c", "/a/b/c"},
|
||||
|
||||
// Remove doubled slash
|
||||
{"//", "/"},
|
||||
{"/abc//", "/abc/"},
|
||||
{"/abc/def//", "/abc/def/"},
|
||||
{"/a/b/c//", "/a/b/c/"},
|
||||
{"/abc//def//ghi", "/abc/def/ghi"},
|
||||
{"//abc", "/abc"},
|
||||
{"///abc", "/abc"},
|
||||
{"//abc//", "/abc/"},
|
||||
|
||||
// Remove . elements
|
||||
{".", "/"},
|
||||
{"./", "/"},
|
||||
{"/abc/./def", "/abc/def"},
|
||||
{"/./abc/def", "/abc/def"},
|
||||
{"/abc/.", "/abc/"},
|
||||
|
||||
// Remove .. elements
|
||||
{"..", "/"},
|
||||
{"../", "/"},
|
||||
{"../../", "/"},
|
||||
{"../..", "/"},
|
||||
{"../../abc", "/abc"},
|
||||
{"/abc/def/ghi/../jkl", "/abc/def/jkl"},
|
||||
{"/abc/def/../ghi/../jkl", "/abc/jkl"},
|
||||
{"/abc/def/..", "/abc"},
|
||||
{"/abc/def/../..", "/"},
|
||||
{"/abc/def/../../..", "/"},
|
||||
{"/abc/def/../../..", "/"},
|
||||
{"/abc/def/../../../ghi/jkl/../../../mno", "/mno"},
|
||||
|
||||
// Combinations
|
||||
{"abc/./../def", "/def"},
|
||||
{"abc//./../def", "/def"},
|
||||
{"abc/../../././../def", "/def"},
|
||||
}
|
||||
|
||||
func TestPathClean(t *testing.T) {
|
||||
for _, test := range cleanTests {
|
||||
assert.Equal(t, test.result, cleanPath(test.path))
|
||||
assert.Equal(t, test.result, cleanPath(test.result))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPathCleanMallocs(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping malloc count in short mode")
|
||||
}
|
||||
|
||||
for _, test := range cleanTests {
|
||||
allocs := testing.AllocsPerRun(100, func() { cleanPath(test.result) })
|
||||
assert.EqualValues(t, allocs, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPathClean(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, test := range cleanTests {
|
||||
cleanPath(test.path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func genLongPaths() (testPaths []cleanPathTest) {
|
||||
for i := 1; i <= 1234; i++ {
|
||||
ss := strings.Repeat("a", i)
|
||||
|
||||
correctPath := "/" + ss
|
||||
testPaths = append(testPaths, cleanPathTest{
|
||||
path: correctPath,
|
||||
result: correctPath,
|
||||
}, cleanPathTest{
|
||||
path: ss,
|
||||
result: correctPath,
|
||||
}, cleanPathTest{
|
||||
path: "//" + ss,
|
||||
result: correctPath,
|
||||
}, cleanPathTest{
|
||||
path: "/" + ss + "/b/..",
|
||||
result: correctPath,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func TestPathCleanLong(t *testing.T) {
|
||||
cleanTests := genLongPaths()
|
||||
|
||||
for _, test := range cleanTests {
|
||||
assert.Equal(t, test.result, cleanPath(test.path))
|
||||
assert.Equal(t, test.result, cleanPath(test.result))
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPathCleanLong(b *testing.B) {
|
||||
cleanTests := genLongPaths()
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, test := range cleanTests {
|
||||
cleanPath(test.path)
|
||||
}
|
||||
}
|
||||
}
|
120
recovery.go
120
recovery.go
|
@ -1,22 +1,12 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -26,88 +16,7 @@ var (
|
|||
slash = []byte("/")
|
||||
)
|
||||
|
||||
// RecoveryFunc defines the function passable to CustomRecovery.
|
||||
type RecoveryFunc func(c *Context, err any)
|
||||
|
||||
// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
|
||||
func Recovery() HandlerFunc {
|
||||
return RecoveryWithWriter(DefaultErrorWriter)
|
||||
}
|
||||
|
||||
// CustomRecovery returns a middleware that recovers from any panics and calls the provided handle func to handle it.
|
||||
func CustomRecovery(handle RecoveryFunc) HandlerFunc {
|
||||
return RecoveryWithWriter(DefaultErrorWriter, handle)
|
||||
}
|
||||
|
||||
// RecoveryWithWriter returns a middleware for a given writer that recovers from any panics and writes a 500 if there was one.
|
||||
func RecoveryWithWriter(out io.Writer, recovery ...RecoveryFunc) HandlerFunc {
|
||||
if len(recovery) > 0 {
|
||||
return CustomRecoveryWithWriter(out, recovery[0])
|
||||
}
|
||||
return CustomRecoveryWithWriter(out, defaultHandleRecovery)
|
||||
}
|
||||
|
||||
// CustomRecoveryWithWriter returns a middleware for a given writer that recovers from any panics and calls the provided handle func to handle it.
|
||||
func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
||||
var logger *log.Logger
|
||||
if out != nil {
|
||||
logger = log.New(out, "\n\n\x1b[31m", log.LstdFlags)
|
||||
}
|
||||
return func(c *Context) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
// Check for a broken connection, as it is not really a
|
||||
// condition that warrants a panic stack trace.
|
||||
var brokenPipe bool
|
||||
if ne, ok := err.(*net.OpError); ok {
|
||||
var se *os.SyscallError
|
||||
if errors.As(ne, &se) {
|
||||
seStr := strings.ToLower(se.Error())
|
||||
if strings.Contains(seStr, "broken pipe") ||
|
||||
strings.Contains(seStr, "connection reset by peer") {
|
||||
brokenPipe = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if logger != nil {
|
||||
stack := stack(3)
|
||||
httpRequest, _ := httputil.DumpRequest(c.Request, false)
|
||||
headers := strings.Split(string(httpRequest), "\r\n")
|
||||
for idx, header := range headers {
|
||||
current := strings.Split(header, ":")
|
||||
if current[0] == "Authorization" {
|
||||
headers[idx] = current[0] + ": *"
|
||||
}
|
||||
}
|
||||
headersToStr := strings.Join(headers, "\r\n")
|
||||
if brokenPipe {
|
||||
logger.Printf("%s\n%s%s", err, headersToStr, reset)
|
||||
} else if IsDebugging() {
|
||||
logger.Printf("[Recovery] %s panic recovered:\n%s\n%s\n%s%s",
|
||||
timeFormat(time.Now()), headersToStr, err, stack, reset)
|
||||
} else {
|
||||
logger.Printf("[Recovery] %s panic recovered:\n%s\n%s%s",
|
||||
timeFormat(time.Now()), err, stack, reset)
|
||||
}
|
||||
}
|
||||
if brokenPipe {
|
||||
// If the connection is dead, we can't write a status to it.
|
||||
c.Error(err.(error)) //nolint: errcheck
|
||||
c.Abort()
|
||||
} else {
|
||||
handle(c, err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func defaultHandleRecovery(c *Context, err any) {
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// stack returns a nicely formatted stack frame, skipping skip frames.
|
||||
// stack returns a nicely formated stack frame, skipping skip frames
|
||||
func stack(skip int) []byte {
|
||||
buf := new(bytes.Buffer) // the returned data
|
||||
// As we loop, we open files and read them. These variables record the currently
|
||||
|
@ -122,7 +31,7 @@ func stack(skip int) []byte {
|
|||
// Print this much at least. If we can't find the source, it won't show.
|
||||
fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
|
||||
if file != lastFile {
|
||||
data, err := os.ReadFile(file)
|
||||
data, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
@ -156,10 +65,10 @@ func function(pc uintptr) []byte {
|
|||
// runtime/debug.*T·ptrmethod
|
||||
// and want
|
||||
// *T.ptrmethod
|
||||
// Also the package path might contain dot (e.g. code.google.com/...),
|
||||
// Also the package path might contains dot (e.g. code.google.com/...),
|
||||
// so first eliminate the path prefix
|
||||
if lastSlash := bytes.LastIndex(name, slash); lastSlash >= 0 {
|
||||
name = name[lastSlash+1:]
|
||||
if lastslash := bytes.LastIndex(name, slash); lastslash >= 0 {
|
||||
name = name[lastslash+1:]
|
||||
}
|
||||
if period := bytes.Index(name, dot); period >= 0 {
|
||||
name = name[period+1:]
|
||||
|
@ -168,7 +77,18 @@ func function(pc uintptr) []byte {
|
|||
return name
|
||||
}
|
||||
|
||||
// timeFormat returns a customized time string for logger.
|
||||
func timeFormat(t time.Time) string {
|
||||
return t.Format("2006/01/02 - 15:04:05")
|
||||
// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
|
||||
// While Martini is in development mode, Recovery will also output the panic as HTML.
|
||||
func Recovery() HandlerFunc {
|
||||
return func(c *Context) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
stack := stack(3)
|
||||
log.Printf("PANIC: %s\n%s", err, stack)
|
||||
c.Writer.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
}()
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
|
249
recovery_test.go
249
recovery_test.go
|
@ -1,249 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPanicClean(t *testing.T) {
|
||||
buffer := new(bytes.Buffer)
|
||||
router := New()
|
||||
password := "my-super-secret-password"
|
||||
router.Use(RecoveryWithWriter(buffer))
|
||||
router.GET("/recovery", func(c *Context) {
|
||||
c.AbortWithStatus(http.StatusBadRequest)
|
||||
panic("Oupps, Houston, we have a problem")
|
||||
})
|
||||
// RUN
|
||||
w := PerformRequest(router, "GET", "/recovery",
|
||||
header{
|
||||
Key: "Host",
|
||||
Value: "www.google.com",
|
||||
},
|
||||
header{
|
||||
Key: "Authorization",
|
||||
Value: fmt.Sprintf("Bearer %s", password),
|
||||
},
|
||||
header{
|
||||
Key: "Content-Type",
|
||||
Value: "application/json",
|
||||
},
|
||||
)
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
|
||||
// Check the buffer does not have the secret key
|
||||
assert.NotContains(t, buffer.String(), password)
|
||||
}
|
||||
|
||||
// TestPanicInHandler assert that panic has been recovered.
|
||||
func TestPanicInHandler(t *testing.T) {
|
||||
buffer := new(bytes.Buffer)
|
||||
router := New()
|
||||
router.Use(RecoveryWithWriter(buffer))
|
||||
router.GET("/recovery", func(_ *Context) {
|
||||
panic("Oupps, Houston, we have a problem")
|
||||
})
|
||||
// RUN
|
||||
w := PerformRequest(router, "GET", "/recovery")
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, buffer.String(), "panic recovered")
|
||||
assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")
|
||||
assert.Contains(t, buffer.String(), t.Name())
|
||||
assert.NotContains(t, buffer.String(), "GET /recovery")
|
||||
|
||||
// Debug mode prints the request
|
||||
SetMode(DebugMode)
|
||||
// RUN
|
||||
w = PerformRequest(router, "GET", "/recovery")
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, buffer.String(), "GET /recovery")
|
||||
|
||||
SetMode(TestMode)
|
||||
}
|
||||
|
||||
// TestPanicWithAbort assert that panic has been recovered even if context.Abort was used.
|
||||
func TestPanicWithAbort(t *testing.T) {
|
||||
router := New()
|
||||
router.Use(RecoveryWithWriter(nil))
|
||||
router.GET("/recovery", func(c *Context) {
|
||||
c.AbortWithStatus(http.StatusBadRequest)
|
||||
panic("Oupps, Houston, we have a problem")
|
||||
})
|
||||
// RUN
|
||||
w := PerformRequest(router, "GET", "/recovery")
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func TestSource(t *testing.T) {
|
||||
bs := source(nil, 0)
|
||||
assert.Equal(t, dunno, bs)
|
||||
|
||||
in := [][]byte{
|
||||
[]byte("Hello world."),
|
||||
[]byte("Hi, gin.."),
|
||||
}
|
||||
bs = source(in, 10)
|
||||
assert.Equal(t, dunno, bs)
|
||||
|
||||
bs = source(in, 1)
|
||||
assert.Equal(t, []byte("Hello world."), bs)
|
||||
}
|
||||
|
||||
func TestFunction(t *testing.T) {
|
||||
bs := function(1)
|
||||
assert.Equal(t, dunno, bs)
|
||||
}
|
||||
|
||||
// TestPanicWithBrokenPipe asserts that recovery specifically handles
|
||||
// writing responses to broken pipes
|
||||
func TestPanicWithBrokenPipe(t *testing.T) {
|
||||
const expectCode = 204
|
||||
|
||||
expectMsgs := map[syscall.Errno]string{
|
||||
syscall.EPIPE: "broken pipe",
|
||||
syscall.ECONNRESET: "connection reset by peer",
|
||||
}
|
||||
|
||||
for errno, expectMsg := range expectMsgs {
|
||||
t.Run(expectMsg, func(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
router := New()
|
||||
router.Use(RecoveryWithWriter(&buf))
|
||||
router.GET("/recovery", func(c *Context) {
|
||||
// Start writing response
|
||||
c.Header("X-Test", "Value")
|
||||
c.Status(expectCode)
|
||||
|
||||
// Oops. Client connection closed
|
||||
e := &net.OpError{Err: &os.SyscallError{Err: errno}}
|
||||
panic(e)
|
||||
})
|
||||
// RUN
|
||||
w := PerformRequest(router, "GET", "/recovery")
|
||||
// TEST
|
||||
assert.Equal(t, expectCode, w.Code)
|
||||
assert.Contains(t, strings.ToLower(buf.String()), expectMsg)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCustomRecoveryWithWriter(t *testing.T) {
|
||||
errBuffer := new(bytes.Buffer)
|
||||
buffer := new(bytes.Buffer)
|
||||
router := New()
|
||||
handleRecovery := func(c *Context, err any) {
|
||||
errBuffer.WriteString(err.(string))
|
||||
c.AbortWithStatus(http.StatusBadRequest)
|
||||
}
|
||||
router.Use(CustomRecoveryWithWriter(buffer, handleRecovery))
|
||||
router.GET("/recovery", func(_ *Context) {
|
||||
panic("Oupps, Houston, we have a problem")
|
||||
})
|
||||
// RUN
|
||||
w := PerformRequest(router, "GET", "/recovery")
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
assert.Contains(t, buffer.String(), "panic recovered")
|
||||
assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")
|
||||
assert.Contains(t, buffer.String(), t.Name())
|
||||
assert.NotContains(t, buffer.String(), "GET /recovery")
|
||||
|
||||
// Debug mode prints the request
|
||||
SetMode(DebugMode)
|
||||
// RUN
|
||||
w = PerformRequest(router, "GET", "/recovery")
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
assert.Contains(t, buffer.String(), "GET /recovery")
|
||||
|
||||
assert.Equal(t, strings.Repeat("Oupps, Houston, we have a problem", 2), errBuffer.String())
|
||||
|
||||
SetMode(TestMode)
|
||||
}
|
||||
|
||||
func TestCustomRecovery(t *testing.T) {
|
||||
errBuffer := new(bytes.Buffer)
|
||||
buffer := new(bytes.Buffer)
|
||||
router := New()
|
||||
DefaultErrorWriter = buffer
|
||||
handleRecovery := func(c *Context, err any) {
|
||||
errBuffer.WriteString(err.(string))
|
||||
c.AbortWithStatus(http.StatusBadRequest)
|
||||
}
|
||||
router.Use(CustomRecovery(handleRecovery))
|
||||
router.GET("/recovery", func(_ *Context) {
|
||||
panic("Oupps, Houston, we have a problem")
|
||||
})
|
||||
// RUN
|
||||
w := PerformRequest(router, "GET", "/recovery")
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
assert.Contains(t, buffer.String(), "panic recovered")
|
||||
assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")
|
||||
assert.Contains(t, buffer.String(), t.Name())
|
||||
assert.NotContains(t, buffer.String(), "GET /recovery")
|
||||
|
||||
// Debug mode prints the request
|
||||
SetMode(DebugMode)
|
||||
// RUN
|
||||
w = PerformRequest(router, "GET", "/recovery")
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
assert.Contains(t, buffer.String(), "GET /recovery")
|
||||
|
||||
assert.Equal(t, strings.Repeat("Oupps, Houston, we have a problem", 2), errBuffer.String())
|
||||
|
||||
SetMode(TestMode)
|
||||
}
|
||||
|
||||
func TestRecoveryWithWriterWithCustomRecovery(t *testing.T) {
|
||||
errBuffer := new(bytes.Buffer)
|
||||
buffer := new(bytes.Buffer)
|
||||
router := New()
|
||||
DefaultErrorWriter = buffer
|
||||
handleRecovery := func(c *Context, err any) {
|
||||
errBuffer.WriteString(err.(string))
|
||||
c.AbortWithStatus(http.StatusBadRequest)
|
||||
}
|
||||
router.Use(RecoveryWithWriter(DefaultErrorWriter, handleRecovery))
|
||||
router.GET("/recovery", func(_ *Context) {
|
||||
panic("Oupps, Houston, we have a problem")
|
||||
})
|
||||
// RUN
|
||||
w := PerformRequest(router, "GET", "/recovery")
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
assert.Contains(t, buffer.String(), "panic recovered")
|
||||
assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")
|
||||
assert.Contains(t, buffer.String(), t.Name())
|
||||
assert.NotContains(t, buffer.String(), "GET /recovery")
|
||||
|
||||
// Debug mode prints the request
|
||||
SetMode(DebugMode)
|
||||
// RUN
|
||||
w = PerformRequest(router, "GET", "/recovery")
|
||||
// TEST
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
assert.Contains(t, buffer.String(), "GET /recovery")
|
||||
|
||||
assert.Equal(t, strings.Repeat("Oupps, Houston, we have a problem", 2), errBuffer.String())
|
||||
|
||||
SetMode(TestMode)
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
// Copyright 2021 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !go1.18
|
||||
// +build !go1.18
|
||||
|
||||
package render
|
||||
|
||||
type any = interface{}
|
|
@ -1,25 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package render
|
||||
|
||||
import "net/http"
|
||||
|
||||
// Data contains ContentType and bytes data.
|
||||
type Data struct {
|
||||
ContentType string
|
||||
Data []byte
|
||||
}
|
||||
|
||||
// Render (Data) writes data with custom ContentType.
|
||||
func (r Data) Render(w http.ResponseWriter) (err error) {
|
||||
r.WriteContentType(w)
|
||||
_, err = w.Write(r.Data)
|
||||
return
|
||||
}
|
||||
|
||||
// WriteContentType (Data) writes custom ContentType.
|
||||
func (r Data) WriteContentType(w http.ResponseWriter) {
|
||||
writeContentType(w, []string{r.ContentType})
|
||||
}
|
|
@ -1,92 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package render
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Delims represents a set of Left and Right delimiters for HTML template rendering.
|
||||
type Delims struct {
|
||||
// Left delimiter, defaults to {{.
|
||||
Left string
|
||||
// Right delimiter, defaults to }}.
|
||||
Right string
|
||||
}
|
||||
|
||||
// HTMLRender interface is to be implemented by HTMLProduction and HTMLDebug.
|
||||
type HTMLRender interface {
|
||||
// Instance returns an HTML instance.
|
||||
Instance(string, any) Render
|
||||
}
|
||||
|
||||
// HTMLProduction contains template reference and its delims.
|
||||
type HTMLProduction struct {
|
||||
Template *template.Template
|
||||
Delims Delims
|
||||
}
|
||||
|
||||
// HTMLDebug contains template delims and pattern and function with file list.
|
||||
type HTMLDebug struct {
|
||||
Files []string
|
||||
Glob string
|
||||
Delims Delims
|
||||
FuncMap template.FuncMap
|
||||
}
|
||||
|
||||
// HTML contains template reference and its name with given interface object.
|
||||
type HTML struct {
|
||||
Template *template.Template
|
||||
Name string
|
||||
Data any
|
||||
}
|
||||
|
||||
var htmlContentType = []string{"text/html; charset=utf-8"}
|
||||
|
||||
// Instance (HTMLProduction) returns an HTML instance which it realizes Render interface.
|
||||
func (r HTMLProduction) Instance(name string, data any) Render {
|
||||
return HTML{
|
||||
Template: r.Template,
|
||||
Name: name,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
// Instance (HTMLDebug) returns an HTML instance which it realizes Render interface.
|
||||
func (r HTMLDebug) Instance(name string, data any) Render {
|
||||
return HTML{
|
||||
Template: r.loadTemplate(),
|
||||
Name: name,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
func (r HTMLDebug) loadTemplate() *template.Template {
|
||||
if r.FuncMap == nil {
|
||||
r.FuncMap = template.FuncMap{}
|
||||
}
|
||||
if len(r.Files) > 0 {
|
||||
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFiles(r.Files...))
|
||||
}
|
||||
if r.Glob != "" {
|
||||
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob))
|
||||
}
|
||||
panic("the HTML debug render was created without files or glob pattern")
|
||||
}
|
||||
|
||||
// Render (HTML) executes template and writes its result with custom ContentType for response.
|
||||
func (r HTML) Render(w http.ResponseWriter) error {
|
||||
r.WriteContentType(w)
|
||||
|
||||
if r.Name == "" {
|
||||
return r.Template.Execute(w, r.Data)
|
||||
}
|
||||
return r.Template.ExecuteTemplate(w, r.Name, r.Data)
|
||||
}
|
||||
|
||||
// WriteContentType (HTML) writes HTML ContentType.
|
||||
func (r HTML) WriteContentType(w http.ResponseWriter) {
|
||||
writeContentType(w, htmlContentType)
|
||||
}
|
193
render/json.go
193
render/json.go
|
@ -1,193 +0,0 @@
|
|||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package render
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
"git.internal/re/gin/internal/bytesconv"
|
||||
"git.internal/re/gin/internal/json"
|
||||
)
|
||||
|
||||
// JSON contains the given interface object.
|
||||
type JSON struct {
|
||||
Data any
|
||||
}
|
||||
|
||||
// IndentedJSON contains the given interface object.
|
||||
type IndentedJSON struct {
|
||||
Data any
|
||||
}
|
||||
|
||||
// SecureJSON contains the given interface object and its prefix.
|
||||
type SecureJSON struct {
|
||||
Prefix string
|
||||
Data any
|
||||
}
|
||||
|
||||
// JsonpJSON contains the given interface object its callback.
|
||||
type JsonpJSON struct {
|
||||
Callback string
|
||||
Data any
|
||||
}
|
||||
|
||||
// AsciiJSON contains the given interface object.
|
||||
type AsciiJSON struct {
|
||||
Data any
|
||||
}
|
||||
|
||||
// PureJSON contains the given interface object.
|
||||
type PureJSON struct {
|
||||
Data any
|
||||
}
|
||||
|
||||
var (
|
||||
jsonContentType = []string{"application/json; charset=utf-8"}
|
||||
jsonpContentType = []string{"application/javascript; charset=utf-8"}
|
||||
jsonASCIIContentType = []string{"application/json"}
|
||||
)
|
||||
|
||||
// Render (JSON) writes data with custom ContentType.
|
||||
func (r JSON) Render(w http.ResponseWriter) (err error) {
|
||||
if err = WriteJSON(w, r.Data); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// WriteContentType (JSON) writes JSON ContentType.
|
||||
func (r JSON) WriteContentType(w http.ResponseWriter) {
|
||||
writeContentType(w, jsonContentType)
|
||||
}
|
||||
|
||||
// WriteJSON marshals the given interface object and writes it with custom ContentType.
|
||||
func WriteJSON(w http.ResponseWriter, obj any) error {
|
||||
writeContentType(w, jsonContentType)
|
||||
jsonBytes, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(jsonBytes)
|
||||
return err
|
||||
}
|
||||
|
||||
// Render (IndentedJSON) marshals the given interface object and writes it with custom ContentType.
|
||||
func (r IndentedJSON) Render(w http.ResponseWriter) error {
|
||||
r.WriteContentType(w)
|
||||
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(jsonBytes)
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteContentType (IndentedJSON) writes JSON ContentType.
|
||||
func (r IndentedJSON) WriteContentType(w http.ResponseWriter) {
|
||||
writeContentType(w, jsonContentType)
|
||||
}
|
||||
|
||||
// Render (SecureJSON) marshals the given interface object and writes it with custom ContentType.
|
||||
func (r SecureJSON) Render(w http.ResponseWriter) error {
|
||||
r.WriteContentType(w)
|
||||
jsonBytes, err := json.Marshal(r.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// if the jsonBytes is array values
|
||||
if bytes.HasPrefix(jsonBytes, bytesconv.StringToBytes("[")) && bytes.HasSuffix(jsonBytes,
|
||||
bytesconv.StringToBytes("]")) {
|
||||
if _, err = w.Write(bytesconv.StringToBytes(r.Prefix)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, err = w.Write(jsonBytes)
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteContentType (SecureJSON) writes JSON ContentType.
|
||||
func (r SecureJSON) WriteContentType(w http.ResponseWriter) {
|
||||
writeContentType(w, jsonContentType)
|
||||
}
|
||||
|
||||
// Render (JsonpJSON) marshals the given interface object and writes it and its callback with custom ContentType.
|
||||
func (r JsonpJSON) Render(w http.ResponseWriter) (err error) {
|
||||
r.WriteContentType(w)
|
||||
ret, err := json.Marshal(r.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if r.Callback == "" {
|
||||
_, err = w.Write(ret)
|
||||
return err
|
||||
}
|
||||
|
||||
callback := template.JSEscapeString(r.Callback)
|
||||
if _, err = w.Write(bytesconv.StringToBytes(callback)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = w.Write(bytesconv.StringToBytes("(")); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = w.Write(ret); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = w.Write(bytesconv.StringToBytes(");")); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteContentType (JsonpJSON) writes Javascript ContentType.
|
||||
func (r JsonpJSON) WriteContentType(w http.ResponseWriter) {
|
||||
writeContentType(w, jsonpContentType)
|
||||
}
|
||||
|
||||
// Render (AsciiJSON) marshals the given interface object and writes it with custom ContentType.
|
||||
func (r AsciiJSON) Render(w http.ResponseWriter) (err error) {
|
||||
r.WriteContentType(w)
|
||||
ret, err := json.Marshal(r.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var buffer bytes.Buffer
|
||||
for _, r := range bytesconv.BytesToString(ret) {
|
||||
cvt := string(r)
|
||||
if r >= 128 {
|
||||
cvt = fmt.Sprintf("\\u%04x", int64(r))
|
||||
}
|
||||
buffer.WriteString(cvt)
|
||||
}
|
||||
|
||||
_, err = w.Write(buffer.Bytes())
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteContentType (AsciiJSON) writes JSON ContentType.
|
||||
func (r AsciiJSON) WriteContentType(w http.ResponseWriter) {
|
||||
writeContentType(w, jsonASCIIContentType)
|
||||
}
|
||||
|
||||
// Render (PureJSON) writes custom ContentType and encodes the given interface object.
|
||||
func (r PureJSON) Render(w http.ResponseWriter) error {
|
||||
r.WriteContentType(w)
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.SetEscapeHTML(false)
|
||||
return encoder.Encode(r.Data)
|
||||
}
|
||||
|
||||
// WriteContentType (PureJSON) writes custom ContentType.
|
||||
func (r PureJSON) WriteContentType(w http.ResponseWriter) {
|
||||
writeContentType(w, jsonContentType)
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !nomsgpack
|
||||
// +build !nomsgpack
|
||||
|
||||
package render
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/ugorji/go/codec"
|
||||
)
|
||||
|
||||
// Check interface implemented here to support go build tag nomsgpack.
|
||||
// See: https://git.internal/re/gin/pull/1852/
|
||||
var (
|
||||
_ Render = MsgPack{}
|
||||
)
|
||||
|
||||
// MsgPack contains the given interface object.
|
||||
type MsgPack struct {
|
||||
Data any
|
||||
}
|
||||
|
||||
var msgpackContentType = []string{"application/msgpack; charset=utf-8"}
|
||||
|
||||
// WriteContentType (MsgPack) writes MsgPack ContentType.
|
||||
func (r MsgPack) WriteContentType(w http.ResponseWriter) {
|
||||
writeContentType(w, msgpackContentType)
|
||||
}
|
||||
|
||||
// Render (MsgPack) encodes the given interface object and writes data with custom ContentType.
|
||||
func (r MsgPack) Render(w http.ResponseWriter) error {
|
||||
return WriteMsgPack(w, r.Data)
|
||||
}
|
||||
|
||||
// WriteMsgPack writes MsgPack ContentType and encodes the given interface object.
|
||||
func WriteMsgPack(w http.ResponseWriter, obj any) error {
|
||||
writeContentType(w, msgpackContentType)
|
||||
var mh codec.MsgpackHandle
|
||||
return codec.NewEncoder(w, &mh).Encode(obj)
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
// Copyright 2018 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package render
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// ProtoBuf contains the given interface object.
|
||||
type ProtoBuf struct {
|
||||
Data any
|
||||
}
|
||||
|
||||
var protobufContentType = []string{"application/x-protobuf"}
|
||||
|
||||
// Render (ProtoBuf) marshals the given interface object and writes data with custom ContentType.
|
||||
func (r ProtoBuf) Render(w http.ResponseWriter) error {
|
||||
r.WriteContentType(w)
|
||||
|
||||
bytes, err := proto.Marshal(r.Data.(proto.Message))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = w.Write(bytes)
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteContentType (ProtoBuf) writes ProtoBuf ContentType.
|
||||
func (r ProtoBuf) WriteContentType(w http.ResponseWriter) {
|
||||
writeContentType(w, protobufContentType)
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue