iris/plugin_test.go

206 lines
5.6 KiB
Go
Raw Normal View History

// Black-box Testing
package iris_test
2016-07-05 13:37:10 +02:00
import (
"fmt"
"github.com/kataras/iris"
2016-07-05 13:37:10 +02:00
"testing"
)
const (
testPluginExDescription = "Description for My test plugin"
testPluginExName = "My test plugin"
)
type testPluginEx struct {
named, activated, descriptioned bool
prelistenran, postlistenran, precloseran bool
}
func (t *testPluginEx) GetName() string {
fmt.Println("GetName Struct")
t.named = true
return testPluginExName
}
func (t *testPluginEx) GetDescription() string {
fmt.Println("GetDescription Struct")
t.descriptioned = true
return testPluginExDescription
}
func (t *testPluginEx) Activate(p iris.PluginContainer) error {
2016-07-05 13:37:10 +02:00
fmt.Println("Activate Struct")
t.activated = true
return nil
}
func (t *testPluginEx) PreListen(*iris.Framework) {
2016-07-05 13:37:10 +02:00
fmt.Println("PreListen Struct")
t.prelistenran = true
}
func (t *testPluginEx) PostListen(*iris.Framework) {
2016-07-05 13:37:10 +02:00
fmt.Println("PostListen Struct")
t.postlistenran = true
}
func (t *testPluginEx) PreClose(*iris.Framework) {
2016-07-05 13:37:10 +02:00
fmt.Println("PreClose Struct")
t.precloseran = true
}
func ExamplePlugins_Add() {
iris.ResetDefault()
iris.Default.Set(iris.OptionDisableBanner(true))
iris.Plugins.Add(iris.PreListenFunc(func(*iris.Framework) {
2016-07-05 13:37:10 +02:00
fmt.Println("PreListen Func")
}))
iris.Plugins.Add(iris.PostListenFunc(func(*iris.Framework) {
2016-07-05 13:37:10 +02:00
fmt.Println("PostListen Func")
}))
iris.Plugins.Add(iris.PreCloseFunc(func(*iris.Framework) {
2016-07-05 13:37:10 +02:00
fmt.Println("PreClose Func")
}))
myplugin := &testPluginEx{}
iris.Plugins.Add(myplugin)
desc := iris.Plugins.GetDescription(myplugin)
2016-07-05 13:37:10 +02:00
fmt.Println(desc)
// travis have problems if I do that using
// Listen(":8080") and Close()
iris.Plugins.DoPreListen(iris.Default)
iris.Plugins.DoPostListen(iris.Default)
iris.Plugins.DoPreClose(iris.Default)
2016-07-05 13:37:10 +02:00
// Output:
// GetName Struct
// Activate Struct
// GetDescription Struct
// Description for My test plugin
// PreListen Func
// PreListen Struct
// PostListen Func
// PostListen Struct
// PreClose Func
// PreClose Struct
}
// if a plugin has GetName, then it should be registered only one time, the name exists for that reason, it's like unique ID
func TestPluginDublicateName(t *testing.T) {
iris.ResetDefault()
var plugins = iris.Default.Plugins
2016-07-05 13:37:10 +02:00
firstNamedPlugin := &testPluginEx{}
sameNamedPlugin := &testPluginEx{}
// err := plugins.Add(firstNamedPlugin, sameNamedPlugin) or
err := plugins.Add(firstNamedPlugin)
if err != nil {
t.Fatalf("Unexpected error when adding a plugin with name: %s", testPluginExName)
}
err = plugins.Add(sameNamedPlugin)
if err == nil {
t.Fatalf("Expected an error because of dublicate named plugin!")
}
if plugins.Len() != 1 {
t.Fatalf("Expected: %d activated plugin but we got: %d", 1, plugins.Len())
2016-07-05 13:37:10 +02:00
}
}
type testPluginActivationType struct {
shouldError bool
}
func (t testPluginActivationType) Activate(p iris.PluginContainer) error {
2016-07-05 13:37:10 +02:00
p.Add(&testPluginEx{})
if t.shouldError {
return fmt.Errorf("An error happens, this plugin and the added plugins by this plugin should not be registered")
}
return nil
}
2016-12-12 11:18:59 +01:00
// a plugin may contain children plugins too, but,
// if an error happened then all of them are not activated/added to the plugin container
func AddPluginTo(t *testing.T, plugins iris.PluginContainer, plugin iris.Plugin, expectingCount int) {
plugins.Add(plugin)
if plugins.Len() != expectingCount { // 2 because it registeres a second plugin also
t.Fatalf("Expected activated plugins to be: %d but we got: %d", expectingCount, plugins.Len())
2016-07-05 13:37:10 +02:00
}
}
2016-12-12 11:18:59 +01:00
// if any error returned from the Activate plugin's method,
// then this plugin and the plugins it registers should not be registered at all
func TestPluginActivate(t *testing.T) {
iris.ResetDefault()
2016-12-12 11:18:59 +01:00
plugins := iris.Plugins
2016-07-05 13:37:10 +02:00
2016-12-12 11:18:59 +01:00
myValidPluginWithChild := testPluginActivationType{shouldError: false}
AddPluginTo(t, plugins, myValidPluginWithChild, 2) // 2 because its children registered also (its parent is not throwing an error)
myInvalidPlugin := testPluginActivationType{shouldError: true}
// should stay 2, even if we tried to add a new one,
// because it cancels the registration of its children too (shouldError = true)
AddPluginTo(t, plugins, myInvalidPlugin, 2)
2016-07-05 13:37:10 +02:00
}
func TestPluginEvents(t *testing.T) {
iris.ResetDefault()
var plugins = iris.Default.Plugins
2016-07-05 13:37:10 +02:00
var prelistenran, postlistenran, precloseran bool
plugins.Add(iris.PreListenFunc(func(*iris.Framework) {
2016-07-05 13:37:10 +02:00
prelistenran = true
}))
plugins.Add(iris.PostListenFunc(func(*iris.Framework) {
2016-07-05 13:37:10 +02:00
postlistenran = true
}))
plugins.Add(iris.PreCloseFunc(func(*iris.Framework) {
2016-07-05 13:37:10 +02:00
precloseran = true
}))
myplugin := &testPluginEx{}
plugins.Add(myplugin)
if plugins.Len() != 4 {
t.Fatalf("Expected: %d plugins to be registed but we got: %d", 4, plugins.Len())
2016-07-05 13:37:10 +02:00
}
desc := plugins.GetDescription(myplugin)
if desc != testPluginExDescription {
t.Fatalf("Expected: %s as Description of the plugin but got: %s", testPluginExDescription, desc)
}
plugins.DoPreListen(nil)
plugins.DoPostListen(nil)
plugins.DoPreClose(nil)
if !prelistenran {
t.Fatalf("Expected to run PreListen Func but it doesn't!")
2016-07-05 13:37:10 +02:00
}
if !postlistenran {
t.Fatalf("Expected to run PostListen Func but it doesn't!")
2016-07-05 13:37:10 +02:00
}
if !precloseran {
t.Fatalf("Expected to run PostListen Func but it doesn't!")
2016-07-05 13:37:10 +02:00
}
if !myplugin.named {
t.Fatalf("Plugin should be named with: %s!", testPluginExName)
}
if !myplugin.activated {
t.Fatalf("Plugin should be activated but it's not!")
}
if !myplugin.prelistenran {
t.Fatalf("Expected to run PreListen Struct but it doesn't!")
2016-07-05 13:37:10 +02:00
}
if !myplugin.postlistenran {
t.Fatalf("Expected to run PostListen Struct but it doesn't!")
2016-07-05 13:37:10 +02:00
}
if !myplugin.precloseran {
t.Fatalf("Expected to run PostListen Struct but it doesn't!")
2016-07-05 13:37:10 +02:00
}
}