Skip to content

Commit

Permalink
take response status as path param
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanhettige committed May 15, 2023
1 parent 09cee66 commit d582370
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# httprespond
Go webhook that throws http errors to test http exception handling against
Go server that returns http errors to test http exception handling against.

Request /400 for a 400 response, /300 for a 300 response etc.
19 changes: 0 additions & 19 deletions internal/responses/responses.go

This file was deleted.

26 changes: 21 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
package main

import (
"fmt"
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"github.com/nathanhettige/httprespond/internal/responses"
)

func main() {
router := gin.Default()
router.Any("/:input", httpResponse)
router.Run(":3000")
}

router.Any("/200", responses.TwoHundred)
router.Any("/300", responses.ThreeHundred)
router.Any("/400", responses.FourHundred)
func httpResponse(c *gin.Context) {
input := c.Param("input")

router.Run(":3000")
status, err := strconv.Atoi(input)
if err != nil {
c.String(http.StatusNotAcceptable, fmt.Sprintf("Invalid http response status code - %s.", input))
return
}

if http.StatusText(status) == "" {
c.String(http.StatusNotAcceptable, fmt.Sprintf("HTTP Status code %d doesn't exist.", status))
return
}

c.String(status, fmt.Sprintf("%d %s", status, http.StatusText(status)))
}

0 comments on commit d582370

Please sign in to comment.