github.com/wangkui503/aero@v1.0.0/Application_test.go (about) 1 package aero_test 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 "os" 8 "syscall" 9 "testing" 10 "time" 11 12 "github.com/aerogo/aero" 13 "github.com/aerogo/http/client" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 const helloWorld = "Hello World" 18 19 func TestApplicationGet(t *testing.T) { 20 app := aero.New() 21 22 // Register route 23 app.Get("/", func(ctx *aero.Context) string { 24 return ctx.Text(helloWorld) 25 }) 26 27 // Get response 28 response := getResponse(app, "/") 29 30 // Verify response 31 assert.Equal(t, http.StatusOK, response.Code) 32 assert.Equal(t, helloWorld, response.Body.String()) 33 } 34 35 func TestApplicationPost(t *testing.T) { 36 app := aero.New() 37 38 // Register route 39 app.Post("/", func(ctx *aero.Context) string { 40 return ctx.Text(helloWorld) 41 }) 42 43 // Get response 44 request, _ := http.NewRequest("POST", "/", nil) 45 response := httptest.NewRecorder() 46 app.Handler().ServeHTTP(response, request) 47 48 // Verify response 49 assert.Equal(t, http.StatusOK, response.Code) 50 assert.Equal(t, helloWorld, response.Body.String()) 51 } 52 53 func TestApplicationRewrite(t *testing.T) { 54 app := aero.New() 55 56 // Register route 57 app.Get("/hello", func(ctx *aero.Context) string { 58 return ctx.Text(helloWorld) 59 }) 60 61 // Rewrite route 62 app.Rewrite(func(ctx *aero.RewriteContext) { 63 if ctx.URI() == "/" { 64 ctx.SetURI("/hello") 65 return 66 } 67 }) 68 69 // Get response 70 response := getResponse(app, "/") 71 72 // Verify response 73 assert.Equal(t, http.StatusOK, response.Code) 74 assert.Equal(t, helloWorld, response.Body.String()) 75 } 76 77 func TestApplicationLoadConfig(t *testing.T) { 78 app := aero.New() 79 workingDirectory, _ := os.Getwd() 80 81 os.Chdir("test") 82 app.Load() 83 os.Chdir(workingDirectory) 84 85 assert.Equal(t, "Test title", app.Config.Title) 86 } 87 88 func TestApplicationRun(t *testing.T) { 89 app := aero.New() 90 91 // When frontpage is requested, kill the server 92 app.Get("/", func(ctx *aero.Context) string { 93 return ctx.HTML(helloWorld) 94 }) 95 96 // When the server is started, we request the frontpage 97 app.OnStart(func() { 98 client.Get(fmt.Sprintf("http://localhost:%d/", app.Config.Ports.HTTP)).End() 99 syscall.Kill(syscall.Getpid(), syscall.SIGTERM) 100 }) 101 102 // When the server ends, check elapsed time 103 app.OnEnd(func() { 104 elapsed := time.Since(app.StartTime()) 105 assert.True(t, elapsed < 2*time.Second) 106 }) 107 108 // Run 109 app.Run() 110 } 111 112 func TestApplicationRunHTTPS(t *testing.T) { 113 app := aero.New() 114 app.Security.Load("test/fullchain.pem", "test/privkey.pem") 115 116 // Register route 117 app.Get("/", func(ctx *aero.Context) string { 118 return ctx.HTML(helloWorld) 119 }) 120 121 // When the server is started, we request the frontpage 122 app.OnStart(func() { 123 client.Get(fmt.Sprintf("http://localhost:%d/", app.Config.Ports.HTTP)).End() 124 client.Get(fmt.Sprintf("https://localhost:%d/", app.Config.Ports.HTTPS)).End() 125 126 go func() { 127 syscall.Kill(syscall.Getpid(), syscall.SIGTERM) 128 }() 129 }) 130 131 // Run 132 app.Run() 133 } 134 135 func TestApplicationUnavailablePort(t *testing.T) { 136 defer func() { 137 r := recover() 138 assert.NotNil(t, r) 139 assert.Contains(t, r.(error).Error(), "bind: permission denied") 140 }() 141 142 app := aero.New() 143 app.Config.Ports.HTTP = 80 144 app.Config.Ports.HTTPS = 443 145 app.ListenAndServe() 146 } 147 148 // getResponse sends a request to the server and returns the response. 149 func getResponse(app *aero.Application, route string) *httptest.ResponseRecorder { 150 // Create request 151 request, _ := http.NewRequest("GET", route, nil) 152 request.Header.Set("Accept-Encoding", "gzip") 153 154 // Get response 155 response := httptest.NewRecorder() 156 app.Handler().ServeHTTP(response, request) 157 158 return response 159 }