Permalink
Browse files

Move fake app into a separate test file.

  • Loading branch information...
robfig committed May 19, 2013
1 parent c5a8d25 commit 2d8c93c738ed83c5520d379eb63c78d08832f4bd
Showing with 105 additions and 102 deletions.
  1. +105 −0 fakeapp_test.go
  2. +0 −102 server_test.go
View
@@ -0,0 +1,105 @@
package revel
import (
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"reflect"
)
type Hotel struct {
HotelId int
Name, Address string
City, State, Zip string
Country string
Price int
}
type Hotels struct {
*Controller
}
type Static struct {
*Controller
}
func (c Hotels) Show(id int) Result {
title := "View Hotel"
hotel := &Hotel{id, "A Hotel", "300 Main St.", "New York", "NY", "10010", "USA", 300}
return c.Render(title, hotel)
}
func (c Hotels) Book(id int) Result {
hotel := &Hotel{id, "A Hotel", "300 Main St.", "New York", "NY", "10010", "USA", 300}
return c.RenderJson(hotel)
}
func (c Static) Serve(prefix, filepath string) Result {
var basePath, dirName string
if !path.IsAbs(dirName) {
basePath = BasePath
}
fname := path.Join(basePath, prefix, filepath)
file, err := os.Open(fname)
if os.IsNotExist(err) {
return c.NotFound("")
} else if err != nil {
WARN.Printf("Problem opening file (%s): %s ", fname, err)
return c.NotFound("This was found but not sure why we couldn't open it.")
}
return c.RenderFile(file, "")
}
func startFakeBookingApp() {
Init("", "github.com/robfig/revel/samples/booking", "")
// Disable logging.
TRACE = log.New(ioutil.Discard, "", 0)
INFO = TRACE
WARN = TRACE
ERROR = TRACE
MainRouter = NewRouter("")
routesFile, _ := ioutil.ReadFile(filepath.Join(BasePath, "conf", "routes"))
MainRouter.Routes, _ = parseRoutes("", string(routesFile), false)
MainTemplateLoader = NewTemplateLoader([]string{ViewsPath, path.Join(RevelPath, "templates")})
MainTemplateLoader.Refresh()
RegisterController((*Hotels)(nil),
[]*MethodType{
&MethodType{
Name: "Index",
},
&MethodType{
Name: "Show",
Args: []*MethodArg{
{"id", reflect.TypeOf((*int)(nil))},
},
RenderArgNames: map[int][]string{31: []string{"title", "hotel"}},
},
&MethodType{
Name: "Book",
Args: []*MethodArg{
{"id", reflect.TypeOf((*int)(nil))},
},
},
})
RegisterController((*Static)(nil),
[]*MethodType{
&MethodType{
Name: "Serve",
Args: []*MethodArg{
&MethodArg{Name: "prefix", Type: reflect.TypeOf((*string)(nil))},
&MethodArg{Name: "filepath", Type: reflect.TypeOf((*string)(nil))},
},
RenderArgNames: map[int][]string{},
},
})
plugins.OnAppStart()
}
View
@@ -1,64 +1,12 @@
package revel
import (
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"testing"
)
// A copy of the hotel struct from the sample app.
type Hotel struct {
HotelId int
Name, Address string
City, State, Zip string
Country string
Price int
}
type Hotels struct {
*Controller
}
type Static struct {
*Controller
}
func (c Hotels) Show(id int) Result {
title := "View Hotel"
hotel := &Hotel{id, "A Hotel", "300 Main St.", "New York", "NY", "10010", "USA", 300}
return c.Render(title, hotel)
}
func (c Hotels) Book(id int) Result {
hotel := &Hotel{id, "A Hotel", "300 Main St.", "New York", "NY", "10010", "USA", 300}
return c.RenderJson(hotel)
}
func (c Static) Serve(prefix, filepath string) Result {
var basePath, dirName string
if !path.IsAbs(dirName) {
basePath = BasePath
}
fname := path.Join(basePath, prefix, filepath)
file, err := os.Open(fname)
if os.IsNotExist(err) {
return c.NotFound("")
} else if err != nil {
WARN.Printf("Problem opening file (%s): %s ", fname, err)
return c.NotFound("This was found but not sure why we couldn't open it.")
}
return c.RenderFile(file, "")
}
// This tries to benchmark the usual request-serving pipeline to get an overall
// performance metric.
//
@@ -129,53 +77,3 @@ var (
staticRequest, _ = http.NewRequest("GET", "/public/js/sessvars.js", nil)
jsonRequest, _ = http.NewRequest("GET", "/hotels/3/booking", nil)
)
func startFakeBookingApp() {
Init("", "github.com/robfig/revel/samples/booking", "")
// Disable logging.
TRACE = log.New(ioutil.Discard, "", 0)
INFO = TRACE
WARN = TRACE
ERROR = TRACE
MainRouter = NewRouter("")
routesFile, _ := ioutil.ReadFile(filepath.Join(BasePath, "conf", "routes"))
MainRouter.Routes, _ = parseRoutes("", string(routesFile), false)
MainTemplateLoader = NewTemplateLoader([]string{ViewsPath, path.Join(RevelPath, "templates")})
MainTemplateLoader.Refresh()
RegisterController((*Hotels)(nil),
[]*MethodType{
&MethodType{
Name: "Index",
},
&MethodType{
Name: "Show",
Args: []*MethodArg{
{"id", reflect.TypeOf((*int)(nil))},
},
RenderArgNames: map[int][]string{36: []string{"title", "hotel"}},
},
&MethodType{
Name: "Book",
Args: []*MethodArg{
{"id", reflect.TypeOf((*int)(nil))},
},
},
})
RegisterController((*Static)(nil),
[]*MethodType{
&MethodType{
Name: "Serve",
Args: []*MethodArg{
&MethodArg{Name: "prefix", Type: reflect.TypeOf((*string)(nil))},
&MethodArg{Name: "filepath", Type: reflect.TypeOf((*string)(nil))},
},
RenderArgNames: map[int][]string{},
},
})
plugins.OnAppStart()
}

0 comments on commit 2d8c93c

Please sign in to comment.