From f95986d0c0b6481603ed967b88fec1d1847704c2 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sun, 22 Oct 2017 01:12:12 +0300 Subject: [PATCH] Nothing crazy, yet. Add an `iris.StaticEmbeddedHandler` shortcut of `router.StaticEmbeddedHandler` and extend the configuration's tests Former-commit-id: 7a105c12ffe08f071bff2c212f96d648ed58c542 --- README_NEXT.md | 1 - configuration_test.go | 75 +++++++++++++++++++++++++++++++++++++++++-- iris.go | 6 ++++ 3 files changed, 78 insertions(+), 4 deletions(-) diff --git a/README_NEXT.md b/README_NEXT.md index dfb0205a..a6e862a9 100644 --- a/README_NEXT.md +++ b/README_NEXT.md @@ -421,7 +421,6 @@ Join the welcoming community of fellow _iris_ developers in [rocket.chat](https: - :star: and watch the public [repository](https://github.com/kataras/iris/stargazers), will keep you up to date - :earth_americas: publish [an article](https://medium.com/search?q=iris) or share a [tweet](https://twitter.com/hashtag/golang) about your personal experience with iris. - The most useful community repository for _iris_ developers is the [iris-contrib/middleware](https://github.com/iris-contrib/middleware) which contains some HTTP handlers that can help you finish a lot of your tasks even easier. Feel free to push your own middleware there! diff --git a/configuration_test.go b/configuration_test.go index 6df6b844..c3ae4e38 100644 --- a/configuration_test.go +++ b/configuration_test.go @@ -110,9 +110,16 @@ EnablePathEscape: false FireMethodNotAllowed: true EnableOptimizations: true DisableBodyConsumptionOnUnmarshal: true -TimeFormat: Mon, 01 Jan 2006 15:04:05 GMT -Charset: UTF-8 +TimeFormat: "Mon, 01 Jan 2006 15:04:05 GMT" +Charset: "UTF-8" +RemoteAddrHeaders: + X-Real-Ip: true + X-Forwarded-For: true + CF-Connecting-IP: true + +Other: + MyServerName: "Iris: https://github.com/kataras/iris" ` yamlFile.WriteString(yamlConfigurationContents) filename := yamlFile.Name() @@ -151,6 +158,35 @@ Charset: UTF-8 if expected := "UTF-8"; c.Charset != expected { t.Fatalf("error on TestConfigurationYAML: Expected Charset %s but got %s", expected, c.Charset) } + + if len(c.RemoteAddrHeaders) == 0 { + t.Fatalf("error on TestConfigurationYAML: Expected RemoteAddrHeaders to be filled") + } + + expectedRemoteAddrHeaders := map[string]bool{ + "X-Real-Ip": true, + "X-Forwarded-For": true, + "CF-Connecting-IP": true, + } + + if expected, got := len(c.RemoteAddrHeaders), len(expectedRemoteAddrHeaders); expected != got { + t.Fatalf("error on TestConfigurationYAML: Expected RemoteAddrHeaders' len(%d) and got(%d), len is not the same", expected, got) + } + + for k, v := range c.RemoteAddrHeaders { + if expected, got := expectedRemoteAddrHeaders[k], v; expected != got { + t.Fatalf("error on TestConfigurationYAML: Expected RemoteAddrHeaders[%s] = %t but got %t", k, expected, got) + } + } + + if len(c.Other) == 0 { + t.Fatalf("error on TestConfigurationYAML: Expected Other to be filled") + } + + if expected, got := "Iris: https://github.com/kataras/iris", c.Other["MyServerName"]; expected != got { + t.Fatalf("error on TestConfigurationYAML: Expected Other['MyServerName'] %s but got %s", expected, got) + } + } func TestConfigurationTOML(t *testing.T) { @@ -175,9 +211,14 @@ DisableBodyConsumptionOnUnmarshal = true TimeFormat = "Mon, 01 Jan 2006 15:04:05 GMT" Charset = "UTF-8" +[RemoteAddrHeaders] + X-Real-Ip = true + X-Forwarded-For = true + CF-Connecting-IP = true + [Other] # Indentation (tabs and/or spaces) is allowed but not required - MyServerName = "Iris" + MyServerName = "Iris: https://github.com/kataras/iris" ` tomlFile.WriteString(tomlConfigurationContents) @@ -217,4 +258,32 @@ Charset = "UTF-8" if expected := "UTF-8"; c.Charset != expected { t.Fatalf("error on TestConfigurationTOML: Expected Charset %s but got %s", expected, c.Charset) } + + if len(c.RemoteAddrHeaders) == 0 { + t.Fatalf("error on TestConfigurationTOML: Expected RemoteAddrHeaders to be filled") + } + + expectedRemoteAddrHeaders := map[string]bool{ + "X-Real-Ip": true, + "X-Forwarded-For": true, + "CF-Connecting-IP": true, + } + + if expected, got := len(c.RemoteAddrHeaders), len(expectedRemoteAddrHeaders); expected != got { + t.Fatalf("error on TestConfigurationTOML: Expected RemoteAddrHeaders' len(%d) and got(%d), len is not the same", expected, got) + } + + for k, v := range c.RemoteAddrHeaders { + if expected, got := expectedRemoteAddrHeaders[k], v; expected != got { + t.Fatalf("error on TestConfigurationTOML: Expected RemoteAddrHeaders[%s] = %t but got %t", k, expected, got) + } + } + + if len(c.Other) == 0 { + t.Fatalf("error on TestConfigurationTOML: Expected Other to be filled") + } + + if expected, got := "Iris: https://github.com/kataras/iris", c.Other["MyServerName"]; expected != got { + t.Fatalf("error on TestConfigurationTOML: Expected Other['MyServerName'] %s but got %s", expected, got) + } } diff --git a/iris.go b/iris.go index 660477e2..67292c1e 100644 --- a/iris.go +++ b/iris.go @@ -303,6 +303,12 @@ var ( // // A shortcut for the `context#LimitRequestBodySize`. LimitRequestBodySize = context.LimitRequestBodySize + // StaticEmbeddedHandler returns a Handler which can serve + // embedded into executable files. + // + // + // Examples: https://github.com/kataras/iris/tree/master/_examples/file-server + StaticEmbeddedHandler = router.StaticEmbeddedHandler // Gzip is a middleware which enables writing // using gzip compression, if client supports. //