Skip to content

Commit

Permalink
Merge pull request #229 from nonrational/nip-io
Browse files Browse the repository at this point in the history
Add support for nip.io with test coverage
  • Loading branch information
nateberkopec committed Feb 11, 2020
2 parents ddadeac + 38f4895 commit 69b5794
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ In the case of rails, you need to configure rails to allow all websockets or web

Or you can add something like `config.action_cable.allowed_request_origins = /(\.test$)|^localhost$/` to allow anything under `.test` as well as `localhost`.

### xip.io
### xip.io/nip.io

Puma-dev supports `xip.io` domains. It will detect them and strip them away, so that your `test` app can be accessed as `test.A.B.C.D.xip.io`.
Puma-dev supports `xip.io` and `nip.io` domains. It will detect them and strip them away, so that your `test` app can be accessed as `test.A.B.C.D.xip.io`.

### Run multiple domains

Expand Down
37 changes: 1 addition & 36 deletions dev/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,41 +104,6 @@ func (h *HTTPServer) findApp(name string) (*App, error) {
return app, nil
}

func (h *HTTPServer) hostForApp(name string) (string, string, error) {
var (
app *App
err error
)

for name != "" {
app, err = h.Pool.App(name)
if err != nil {
if err == ErrUnknownApp {
name = pruneSub(name)
continue
}

return "", "", err
}

break
}

if app == nil {
app, err = h.Pool.App("default")
if err != nil {
return "", "", err
}
}

err = app.WaitTilReady()
if err != nil {
return "", "", err
}

return app.Scheme, app.Address(), nil
}

func (h *HTTPServer) removeTLD(host string) string {
colon := strings.LastIndexByte(host, ':')
if colon != -1 {
Expand All @@ -147,7 +112,7 @@ func (h *HTTPServer) removeTLD(host string) string {
}
}

if strings.HasSuffix(host, ".xip.io") {
if strings.HasSuffix(host, ".xip.io") || strings.HasSuffix(host, ".nip.io") {
parts := strings.Split(host, ".")
if len(parts) < 6 {
return ""
Expand Down
51 changes: 51 additions & 0 deletions dev/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package dev

import (
"testing"

"github.com/stretchr/testify/assert"
)

var testHttp HTTPServer

func TestHttp_removeTLD_test(t *testing.T) {
str := testHttp.removeTLD("psychic-octo-guide.test")

assert.Equal(t, "psychic-octo-guide", str)
}

func TestHttp_removeTLD_noTld(t *testing.T) {
str := testHttp.removeTLD("shiny-train")

assert.Equal(t, "shiny-train", str)
}

func TestHttp_removeTLD_mutlipartDomain(t *testing.T) {
str := testHttp.removeTLD("expert-eureka.loc.al")

assert.Equal(t, "expert-eureka.loc", str)
}

func TestHttp_removeTLD_dev(t *testing.T) {
str := testHttp.removeTLD("bookish-giggle.dev:8080")

assert.Equal(t, "bookish-giggle", str)
}

func TestHttp_removeTLD_xipIoMalformed(t *testing.T) {
str := testHttp.removeTLD("legendary-meme.0.0.xip.io")

assert.Equal(t, "", str)
}

func TestHttp_removeTLD_xipIoDots(t *testing.T) {
str := testHttp.removeTLD("legendary-meme.0.0.0.0.xip.io")

assert.Equal(t, "legendary-meme", str)
}

func TestHttp_removeTLD_nipIoDots(t *testing.T) {
str := testHttp.removeTLD("effective-invention.255.255.255.255.nip.io")

assert.Equal(t, "effective-invention", str)
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ github.com/bmizerany/pat v0.0.0-20160217103242-c068ca2f0aac/go.mod h1:8rLXio+Wji
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/fsnotify/fsevents v0.1.0 h1:qeALFWR0ZE27D78Zq8mYxCF/5YrioLzrddiDz5pbR7c=
github.com/fsnotify/fsevents v0.1.0/go.mod h1:+d+hS27T6k5J8CRaPLKFgwKYcpS7GwW3Ule9+SC2ZRc=
github.com/hashicorp/golang-lru v0.0.0-20160207214719-a0d98a5f2880 h1:OaRuzt9oCKNui8cCskZijoKUwe+aCuuCwvx1ox8FNyw=
github.com/hashicorp/golang-lru v0.0.0-20160207214719-a0d98a5f2880/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down

0 comments on commit 69b5794

Please sign in to comment.