Skip to content

Instantly share code, notes, and snippets.

@pratikmallya
Created February 10, 2020 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pratikmallya/97e7c180dedd13d22305c84de64234e8 to your computer and use it in GitHub Desktop.
Save pratikmallya/97e7c180dedd13d22305c84de64234e8 to your computer and use it in GitHub Desktop.
echo server that returns different http status codes
package main
import (
"github.com/labstack/echo"
"net/http"
)
func main() {
e := echo.New()
e.Use(FailDifferently)
e.Logger.Fatal(e.Start(":8080"))
return
}
func FailDifferently(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Path() == "/100" {return echo.NewHTTPError(http.StatusContinue)}
if c.Path() == "/101" {return echo.NewHTTPError(http.StatusSwitchingProtocols)}
if c.Path() == "/102" {return echo.NewHTTPError(http.StatusProcessing)}
if c.Path() == "/103" {return echo.NewHTTPError(http.StatusEarlyHints)}
if c.Path() == "/200" {return echo.NewHTTPError(http.StatusOK)}
if c.Path() == "/201" {return echo.NewHTTPError(http.StatusCreated)}
if c.Path() == "/202" {return echo.NewHTTPError(http.StatusAccepted)}
if c.Path() == "/203" {return echo.NewHTTPError(http.StatusNonAuthoritativeInfo)}
if c.Path() == "/204" {return echo.NewHTTPError(http.StatusNoContent)}
if c.Path() == "/205" {return echo.NewHTTPError(http.StatusResetContent)}
if c.Path() == "/206" {return echo.NewHTTPError(http.StatusPartialContent)}
if c.Path() == "/207" {return echo.NewHTTPError(http.StatusMultiStatus)}
if c.Path() == "/208" {return echo.NewHTTPError(http.StatusAlreadyReported)}
if c.Path() == "/226" {return echo.NewHTTPError(http.StatusIMUsed)}
if c.Path() == "/300" {return echo.NewHTTPError(http.StatusMultipleChoices)}
if c.Path() == "/301" {return echo.NewHTTPError(http.StatusMovedPermanently)}
if c.Path() == "/302" {return echo.NewHTTPError(http.StatusFound)}
if c.Path() == "/303" {return echo.NewHTTPError(http.StatusSeeOther)}
if c.Path() == "/304" {return echo.NewHTTPError(http.StatusNotModified)}
if c.Path() == "/305" {return echo.NewHTTPError(http.StatusUseProxy)}
if c.Path() == "/307" {return echo.NewHTTPError(http.StatusTemporaryRedirect)}
if c.Path() == "/308" {return echo.NewHTTPError(http.StatusPermanentRedirect)}
if c.Path() == "/400" {return echo.NewHTTPError(http.StatusBadRequest)}
if c.Path() == "/401" {return echo.NewHTTPError(http.StatusUnauthorized)}
if c.Path() == "/402" {return echo.NewHTTPError(http.StatusPaymentRequired)}
if c.Path() == "/403" {return echo.NewHTTPError(http.StatusForbidden)}
if c.Path() == "/404" {return echo.NewHTTPError(http.StatusNotFound)}
if c.Path() == "/405" {return echo.NewHTTPError(http.StatusMethodNotAllowed)}
if c.Path() == "/406" {return echo.NewHTTPError(http.StatusNotAcceptable)}
if c.Path() == "/407" {return echo.NewHTTPError(http.StatusProxyAuthRequired)}
if c.Path() == "/408" {return echo.NewHTTPError(http.StatusRequestTimeout)}
if c.Path() == "/409" {return echo.NewHTTPError(http.StatusConflict)}
if c.Path() == "/410" {return echo.NewHTTPError(http.StatusGone)}
if c.Path() == "/411" {return echo.NewHTTPError(http.StatusLengthRequired)}
if c.Path() == "/412" {return echo.NewHTTPError(http.StatusPreconditionFailed)}
if c.Path() == "/413" {return echo.NewHTTPError(http.StatusRequestEntityTooLarge)}
if c.Path() == "/414" {return echo.NewHTTPError(http.StatusRequestURITooLong)}
if c.Path() == "/415" {return echo.NewHTTPError(http.StatusUnsupportedMediaType)}
if c.Path() == "/416" {return echo.NewHTTPError(http.StatusRequestedRangeNotSatisfiable)}
if c.Path() == "/417" {return echo.NewHTTPError(http.StatusExpectationFailed)}
if c.Path() == "/418" {return echo.NewHTTPError(http.StatusTeapot)}
if c.Path() == "/421" {return echo.NewHTTPError(http.StatusMisdirectedRequest)}
if c.Path() == "/422" {return echo.NewHTTPError(http.StatusUnprocessableEntity)}
if c.Path() == "/423" {return echo.NewHTTPError(http.StatusLocked)}
if c.Path() == "/424" {return echo.NewHTTPError(http.StatusFailedDependency)}
if c.Path() == "/425" {return echo.NewHTTPError(http.StatusTooEarly)}
if c.Path() == "/426" {return echo.NewHTTPError(http.StatusUpgradeRequired)}
if c.Path() == "/428" {return echo.NewHTTPError(http.StatusPreconditionRequired)}
if c.Path() == "/429" {return echo.NewHTTPError(http.StatusTooManyRequests)}
if c.Path() == "/431" {return echo.NewHTTPError(http.StatusRequestHeaderFieldsTooLarge)}
if c.Path() == "/451" {return echo.NewHTTPError(http.StatusUnavailableForLegalReasons)}
if c.Path() == "/500" {return echo.NewHTTPError(http.StatusInternalServerError)}
if c.Path() == "/501" {return echo.NewHTTPError(http.StatusNotImplemented)}
if c.Path() == "/502" {return echo.NewHTTPError(http.StatusBadGateway)}
if c.Path() == "/503" {return echo.NewHTTPError(http.StatusServiceUnavailable)}
if c.Path() == "/504" {return echo.NewHTTPError(http.StatusGatewayTimeout)}
if c.Path() == "/505" {return echo.NewHTTPError(http.StatusHTTPVersionNotSupported)}
if c.Path() == "/506" {return echo.NewHTTPError(http.StatusVariantAlsoNegotiates)}
if c.Path() == "/507" {return echo.NewHTTPError(http.StatusInsufficientStorage)}
if c.Path() == "/508" {return echo.NewHTTPError(http.StatusLoopDetected)}
if c.Path() == "/510" {return echo.NewHTTPError(http.StatusNotExtended)}
if c.Path() == "/511" {return echo.NewHTTPError(http.StatusNetworkAuthenticationRequired)}
return next(c)
}
}
@pratikmallya
Copy link
Author

pratikmallya commented Feb 10, 2020

Install

go get

Run

 go run main.go

   ____    __
  / __/___/ /  ___
 / _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.1.14
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
                                    O\
⇨ http server started on [::]:8080

Hit it

~> curl -v 0.0.0.0:8080/401
*   Trying 0.0.0.0...
* TCP_NODELAY set
* Connected to 0.0.0.0 (127.0.0.1) port 8080 (#0)
> GET /401 HTTP/1.1
> Host: 0.0.0.0:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 401 Unauthorized
< Content-Type: application/json; charset=UTF-8
< Date: Mon, 10 Feb 2020 07:30:37 GMT
< Content-Length: 27
<
{"message":"Unauthorized"}
* Connection #0 to host 0.0.0.0 left intact
~> curl -v 0.0.0.0:8080/501
*   Trying 0.0.0.0...
* TCP_NODELAY set
* Connected to 0.0.0.0 (127.0.0.1) port 8080 (#0)
> GET /501 HTTP/1.1
> Host: 0.0.0.0:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 501 Not Implemented
< Content-Type: application/json; charset=UTF-8
< Date: Mon, 10 Feb 2020 07:30:39 GMT
< Content-Length: 30
<
{"message":"Not Implemented"}
* Connection #0 to host 0.0.0.0 left intact

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment