mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
Add a Test framework to make it easier to test Iris app
I will commit the tests , mostly to see how this is working, when I finish with the front-end tests
This commit is contained in:
parent
0d4b0ecd43
commit
126e170371
25
LICENSE
25
LICENSE
|
@ -205,7 +205,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
color -
|
color
|
||||||
Copyright (c) 2016 Gerasimos Maropoulos
|
Copyright (c) 2016 Gerasimos Maropoulos
|
||||||
Copyright (c) 2013 Fatih Arslan
|
Copyright (c) 2013 Fatih Arslan
|
||||||
|
|
||||||
|
@ -675,6 +675,29 @@ i18n - Apache License
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
|
|
||||||
|
|
||||||
|
httpexpect - test framework,
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Victor Gaydov
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
|
||||||
|
|
||||||
Go packages license,
|
Go packages license,
|
||||||
Copyright (c) 2012 The Go Authors. All rights reserved.
|
Copyright (c) 2012 The Go Authors. All rights reserved.
|
||||||
|
|
|
@ -109,6 +109,9 @@ type (
|
||||||
// this field is useful only when you need to READ which is the server's address, certfile & keyfile or unix's mode.
|
// this field is useful only when you need to READ which is the server's address, certfile & keyfile or unix's mode.
|
||||||
//
|
//
|
||||||
Server Server
|
Server Server
|
||||||
|
|
||||||
|
// Tester contains the configs for the test framework, so far we have only one because all test framework's configs are setted by the iris itself
|
||||||
|
Tester Tester
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render struct keeps organise all configuration about rendering, templates and rest currently.
|
// Render struct keeps organise all configuration about rendering, templates and rest currently.
|
||||||
|
@ -146,6 +149,7 @@ func Default() Iris {
|
||||||
Render: DefaultRender(),
|
Render: DefaultRender(),
|
||||||
Websocket: DefaultWebsocket(),
|
Websocket: DefaultWebsocket(),
|
||||||
Server: DefaultServer(),
|
Server: DefaultServer(),
|
||||||
|
Tester: Tester{Debug: false},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
6
config/tester.go
Normal file
6
config/tester.go
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
// Tester configuration
|
||||||
|
type Tester struct {
|
||||||
|
Debug bool
|
||||||
|
}
|
46
iris.go
46
iris.go
|
@ -52,13 +52,16 @@ package iris // import "github.com/kataras/iris"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gavv/httpexpect"
|
||||||
"github.com/iris-contrib/errors"
|
"github.com/iris-contrib/errors"
|
||||||
"github.com/kataras/iris/config"
|
"github.com/kataras/iris/config"
|
||||||
"github.com/kataras/iris/context"
|
"github.com/kataras/iris/context"
|
||||||
|
@ -102,6 +105,7 @@ type (
|
||||||
Path(string, ...interface{}) string
|
Path(string, ...interface{}) string
|
||||||
URL(string, ...interface{}) string
|
URL(string, ...interface{}) string
|
||||||
TemplateString(string, interface{}, ...string) string
|
TemplateString(string, interface{}, ...string) string
|
||||||
|
Tester(t *testing.T) *httpexpect.Expect
|
||||||
}
|
}
|
||||||
|
|
||||||
// RouteNameFunc the func returns from the MuxAPi's methods, optionally sets the name of the Route (*route)
|
// RouteNameFunc the func returns from the MuxAPi's methods, optionally sets the name of the Route (*route)
|
||||||
|
@ -601,6 +605,48 @@ func (s *Framework) TemplateString(templateFile string, pageContext interface{},
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewTester Prepares and returns a new test framework based on the api
|
||||||
|
// is useful when you need to have more than one test framework for the same iris insttance, otherwise you can use the iris.Tester(t *testing.T)/variable.Tester(t *testing.T)
|
||||||
|
func NewTester(api *Framework, t *testing.T) *httpexpect.Expect {
|
||||||
|
api.Config.DisableBanner = true
|
||||||
|
if !api.HTTPServer.IsListening() { // maybe the user called this after .Listen/ListenTLS/ListenUNIX, the tester can be used as standalone (with no running iris instance) or inside a running instance/app
|
||||||
|
api.NoListen()
|
||||||
|
if ok := <-api.Available; !ok {
|
||||||
|
t.Fatal("Unexpected error: server cannot start, please report this as bug!!")
|
||||||
|
}
|
||||||
|
close(api.Available)
|
||||||
|
}
|
||||||
|
|
||||||
|
handler := api.HTTPServer.Handler
|
||||||
|
|
||||||
|
testConfiguration := httpexpect.Config{
|
||||||
|
BaseURL: api.HTTPServer.FullHost(),
|
||||||
|
Client: &http.Client{
|
||||||
|
Transport: httpexpect.NewFastBinder(handler),
|
||||||
|
Jar: httpexpect.NewJar(),
|
||||||
|
},
|
||||||
|
Reporter: httpexpect.NewAssertReporter(t),
|
||||||
|
}
|
||||||
|
|
||||||
|
if api.Config.Tester.Debug {
|
||||||
|
testConfiguration.Printers = []httpexpect.Printer{
|
||||||
|
httpexpect.NewDebugPrinter(t, true),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return httpexpect.WithConfig(testConfiguration)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tester returns the test framework for this default insance
|
||||||
|
func Tester(t *testing.T) *httpexpect.Expect {
|
||||||
|
return Default.Tester(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tester returns the test framework for this iris insance
|
||||||
|
func (s *Framework) Tester(t *testing.T) *httpexpect.Expect {
|
||||||
|
return s.tester(t)
|
||||||
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------------
|
||||||
// -------------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------------
|
||||||
// ----------------------------------MuxAPI implementation------------------------------
|
// ----------------------------------MuxAPI implementation------------------------------
|
||||||
|
|
Loading…
Reference in New Issue
Block a user