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:
Makis Maropoulos 2016-07-03 16:26:53 +02:00
parent 0d4b0ecd43
commit 126e170371
4 changed files with 85 additions and 6 deletions

25
LICENSE
View File

@ -205,7 +205,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The MIT License (MIT)
color -
color
Copyright (c) 2016 Gerasimos Maropoulos
Copyright (c) 2013 Fatih Arslan
@ -675,6 +675,29 @@ i18n - Apache 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,
Copyright (c) 2012 The Go Authors. All rights reserved.

View File

@ -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.
//
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.
@ -146,6 +149,7 @@ func Default() Iris {
Render: DefaultRender(),
Websocket: DefaultWebsocket(),
Server: DefaultServer(),
Tester: Tester{Debug: false},
}
}

6
config/tester.go Normal file
View File

@ -0,0 +1,6 @@
package config
// Tester configuration
type Tester struct {
Debug bool
}

46
iris.go
View File

@ -52,13 +52,16 @@ package iris // import "github.com/kataras/iris"
import (
"fmt"
"net/http"
"os"
"path"
"reflect"
"strconv"
"strings"
"testing"
"time"
"github.com/gavv/httpexpect"
"github.com/iris-contrib/errors"
"github.com/kataras/iris/config"
"github.com/kataras/iris/context"
@ -102,6 +105,7 @@ type (
Path(string, ...interface{}) string
URL(string, ...interface{}) 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)
@ -601,6 +605,48 @@ func (s *Framework) TemplateString(templateFile string, pageContext interface{},
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------------------------------