Skip to content
Myr

router.Run() can be called without parameters. #405

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Manu Mtz-Almeida · committed by Manu Mtz-Almeida · parent 2b3aa51 · Browse files · 3 files+75 added−19 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+75 added−19 removed

  • other:+75 added−19 removed

3 files+75 added−19 removed

modified:gin_integration_test.go+53 added-16 removed
1package gin23import (4 "bufio"5 "bytes"5 "fmt"6 "io/ioutil"7 "net"8 "net/http"9 "os"10 "testing"11 "time"1213 "github.com/stretchr/testify/assert"14)1516func TestRun(t *testing.T) {17 buffer := new(bytes.Buffer)16func testRequest(t *testing.T, url string) {17 resp, err := http.Get(url)18 defer resp.Body.Close()19 assert.NoError(t, err)2021 body, ioerr := ioutil.ReadAll(resp.Body)22 assert.NoError(t, ioerr)23 assert.Equal(t, "it worked", string(body), "resp body should match")24 assert.Equal(t, "200 OK", resp.Status, "should get a 200")25}2627func TestRunEmpty(t *testing.T) {28 SetMode(DebugMode)29 os.Setenv("PORT", "")30 router := New()31 go func() {20 router.Use(LoggerWithWriter(buffer))32 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })22 router.Run(":5150")33 assert.NoError(t, router.Run())34 }()35 // have to wait for the goroutine to start and run the server36 // otherwise the main thread will complete37 time.Sleep(5 * time.Millisecond)3828 assert.Error(t, router.Run(":5150"))39 assert.Error(t, router.Run(":8080"))40 testRequest(t, "http://localhost:8080/example")41}4230 resp, err := http.Get("http://localhost:5150/example")31 defer resp.Body.Close()32 assert.NoError(t, err)43func TestRunEmptyWithEnv(t *testing.T) {44 os.Setenv("PORT", "3123")45 router := New()46 go func() {47 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })48 assert.NoError(t, router.Run())49 }()50 // have to wait for the goroutine to start and run the server51 // otherwise the main thread will complete52 time.Sleep(5 * time.Millisecond)5334 body, ioerr := ioutil.ReadAll(resp.Body)35 assert.NoError(t, ioerr)36 assert.Equal(t, "it worked", string(body[:]), "resp body should match")37 assert.Equal(t, "200 OK", resp.Status, "should get a 200")54 assert.Error(t, router.Run(":3123"))55 testRequest(t, "http://localhost:3123/example")56}5758func TestRunTooMuchParams(t *testing.T) {59 router := New()60 assert.Panics(t, func() {61 router.Run("2", "2")62 })63}6465func TestRunWithPort(t *testing.T) {66 router := New()67 go func() {68 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })69 assert.NoError(t, router.Run(":5150"))70 }()71 // have to wait for the goroutine to start and run the server72 // otherwise the main thread will complete73 time.Sleep(5 * time.Millisecond)7475 assert.Error(t, router.Run(":5150"))76 testRequest(t, "http://localhost:5150/example")77}7879func TestUnixSocket(t *testing.T) {41 buffer := new(bytes.Buffer)80 router := New()8182 go func() {45 router.Use(LoggerWithWriter(buffer))83 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })47 router.RunUnix("/tmp/unix_unit_test")84 assert.NoError(t, router.RunUnix("/tmp/unix_unit_test"))85 }()86 // have to wait for the goroutine to start and run the server87 // otherwise the main thread will complete88 time.Sleep(5 * time.Millisecond)
18 unmodified lines
modified:gin.go+4 added-3 removed
226 unmodified lines
227228// Run attaches the router to a http.Server and starts listening and serving HTTP requests.229// It is a shortcut for http.ListenAndServe(addr, router)230// Note: this method will block the calling goroutine undefinitelly unless an error happens.231func (engine *Engine) Run(addr string) (err error) {232 debugPrint("Listening and serving HTTP on %s\n", addr)231func (engine *Engine) Run(addr ...string) (err error) {232 defer func() { debugPrintError(err) }()233234 address := resolveAddress(addr)235 debugPrint("Listening and serving HTTP on %s\n", address)235 err = http.ListenAndServe(addr, engine)236 err = http.ListenAndServe(address, engine)237 return238}239240// RunTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests.
140 unmodified lines
modified:utils.go+18 added
5 unmodified lines
67import (8 "encoding/xml"9 "net/http"10 "os"11 "path"12 "reflect"13 "runtime"14 "strings"
114 unmodified lines
129 return finalPath + "/"130 }131 return finalPath132}133134func resolveAddress(addr []string) string {135 switch len(addr) {136 case 0:137 if port := os.Getenv("PORT"); len(port) > 0 {138 debugPrint("Environment variable PORT=\"%s\"", port)139 return ":" + port140 } else {141 debugPrint("Environment variable PORT is undefined. Using port :8080 by default")142 return ":8080"143 }144 case 1:145 return addr[0]146 default:147 panic("too much parameters")148 }149}