mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
Add test for Redirect while ListenTLS/ListenLETSENCRYPT
This commit is contained in:
parent
b62302d1d7
commit
0513fa745b
50
README.md
50
README.md
|
@ -30,14 +30,15 @@
|
|||
<br/>
|
||||
|
||||
|
||||
<b>iris</b> is a web application framework written in Go.
|
||||
<b>Easy</b> to <a href="https://github.com/iris-contrib/examples">learn</a>, while it's highly customizable. <br/>
|
||||
<b>Iris</b> is a well known web application framework written in Go.
|
||||
<br/>
|
||||
<b>Easy</b> to <a href="https://github.com/iris-contrib/examples">learn</a>, while it's highly customizable, <br/>
|
||||
<i>ideally suited for both experienced and novice Developers.</i><br/><br/>
|
||||
|
||||
Its only sin?
|
||||
To be the <b>fastest</b> web framework was ever published under open-source circumstances.<br/>
|
||||
<i>Companies' offers were rejected, free forever.</i><br/><br/>
|
||||
|
||||
<b>Enjoy</b> this framework now, without hesitation.<br/>
|
||||
<b>Enjoy yourself!</b><br/>
|
||||
</p>
|
||||
|
||||
|
||||
|
@ -45,22 +46,17 @@ To be the <b>fastest</b> web framework was ever published under open-source circ
|
|||
Quick Start
|
||||
-----------
|
||||
|
||||
The only requirement is the [Go Programming Language](https://golang.org/dl), at least v1.7.
|
||||
The only requirement is the [Go Programming Language](https://golang.org/dl) 1.7+
|
||||
|
||||
```bash
|
||||
$ go get -u github.com/kataras/iris
|
||||
$ go get -u github.com/kataras/iris/iris
|
||||
```
|
||||
|
||||
|
||||
> Production? Install the [stable version](https://github.com/kataras/iris/tree/4.0.0#versioning) instead
|
||||
```bash
|
||||
$ go get -u gopkg.in/kataras/iris.v4
|
||||
```
|
||||
|
||||
### Hello, World!
|
||||
### Hello, JSON!
|
||||
|
||||
```sh
|
||||
$ cat helloworld.go
|
||||
$ cat hellojson.go
|
||||
```
|
||||
|
||||
```go
|
||||
|
@ -70,21 +66,37 @@ import "github.com/kataras/iris"
|
|||
|
||||
func main(){
|
||||
|
||||
iris.Get("/", func(c *iris.Context){
|
||||
c.HTML(iris.StatusOK, "<h1> Hello World! </h1>")
|
||||
// http://localhost:5700/api/user/42
|
||||
// Method: "GET"
|
||||
iris.Get("/api/user/:id", func(ctx *iris.Context){
|
||||
|
||||
// take the :id from the path, parse to integer
|
||||
// and set it to the new userID local variable.
|
||||
userID := ctx.ParamInt("id")
|
||||
|
||||
// userRepo, imaginary database service <- your only job.
|
||||
user := userRepo.GetByID(userID)
|
||||
|
||||
// send back a response to the client,
|
||||
// .JSON: content type as application/json; charset="utf-8"
|
||||
// iris.StatusOK: with 200 http status code.
|
||||
//
|
||||
// send user as it is or make use of any json valid golang type,
|
||||
// like the iris.Map{"username" : user.Username}.
|
||||
ctx.JSON(iris.StatusOK, user)
|
||||
|
||||
})
|
||||
|
||||
|
||||
iris.Listen(":8080")
|
||||
iris.Listen("localhost:5700")
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
```sh
|
||||
$ go run helloworld.go
|
||||
$ go run hellojson.go
|
||||
```
|
||||
|
||||
Open your browser at http://localhost:8080 and you should see the `Hello World!`.
|
||||
Open your browser or any other http client at http://localhost:5700/api/user/42.
|
||||
|
||||
### New
|
||||
|
||||
|
|
10
context.go
10
context.go
|
@ -471,6 +471,10 @@ func (ctx *Context) SetHeader(k string, v string) {
|
|||
ctx.RequestCtx.Response.Header.Set(k, v)
|
||||
}
|
||||
|
||||
// it used only inside Redirect,
|
||||
// keep it here for allocations reason
|
||||
var httpsSchemeOnlyBytes = []byte("https")
|
||||
|
||||
// Redirect redirect sends a redirect response the client
|
||||
// accepts 2 parameters string and an optional int
|
||||
// first parameter is the url to redirect
|
||||
|
@ -486,12 +490,10 @@ func (ctx *Context) Redirect(urlToRedirect string, statusHeader ...int) {
|
|||
|
||||
// #355
|
||||
if ctx.IsTLS() {
|
||||
u := fasthttp.AcquireURI()
|
||||
ctx.URI().CopyTo(u)
|
||||
u.SetScheme("https")
|
||||
u := ctx.URI()
|
||||
u.SetSchemeBytes(httpsSchemeOnlyBytes)
|
||||
u.Update(urlToRedirect)
|
||||
ctx.SetHeader("Location", string(u.FullURI()))
|
||||
fasthttp.ReleaseURI(u)
|
||||
ctx.SetStatusCode(httpStatus)
|
||||
return
|
||||
}
|
||||
|
|
47
http_test.go
47
http_test.go
|
@ -751,3 +751,50 @@ func TestCache(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedirectHTTPS(t *testing.T) {
|
||||
host := "localhost:5700"
|
||||
expectedBody := "Redirected to https://" + host + "/redirected"
|
||||
iris.ResetDefault()
|
||||
defer iris.Close()
|
||||
|
||||
iris.Set(iris.OptionDisableBanner(true))
|
||||
|
||||
iris.Get("/redirect", func(ctx *iris.Context) { ctx.Redirect("/redirected") })
|
||||
iris.Get("/redirected", func(ctx *iris.Context) { ctx.Text(iris.StatusOK, "Redirected to "+ctx.URI().String()) })
|
||||
|
||||
// create the key and cert files on the fly, and delete them when this test finished
|
||||
// note: code dublication but it's ok we may change that to local ListenLETSENCRYPT
|
||||
certFile, ferr := ioutil.TempFile("", "cert")
|
||||
|
||||
if ferr != nil {
|
||||
t.Fatal(ferr.Error())
|
||||
}
|
||||
|
||||
keyFile, ferr := ioutil.TempFile("", "key")
|
||||
if ferr != nil {
|
||||
t.Fatal(ferr.Error())
|
||||
}
|
||||
|
||||
defer func() {
|
||||
certFile.Close()
|
||||
time.Sleep(350 * time.Millisecond)
|
||||
os.Remove(certFile.Name())
|
||||
|
||||
keyFile.Close()
|
||||
time.Sleep(350 * time.Millisecond)
|
||||
os.Remove(keyFile.Name())
|
||||
}()
|
||||
|
||||
certFile.WriteString(testTLSCert)
|
||||
keyFile.WriteString(testTLSKey)
|
||||
|
||||
go iris.ListenTLS(host, certFile.Name(), keyFile.Name())
|
||||
if ok := <-iris.Default.Available; !ok {
|
||||
t.Fatal("Unexpected error: server cannot start, please report this as bug!!")
|
||||
}
|
||||
|
||||
e := httptest.New(iris.Default, t, httptest.ExplicitURL(true))
|
||||
|
||||
e.Request("GET", "https://"+host+"/redirect").Expect().Status(iris.StatusOK).Body().Equal(expectedBody)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user