2018-03-14 06:17:35 +01:00
package main
import (
"bytes"
"io/ioutil"
"path/filepath"
"runtime"
"strings"
"testing"
2019-10-25 00:27:02 +02:00
"github.com/kataras/iris/v12/httptest"
2018-03-14 06:17:35 +01:00
"github.com/klauspost/compress/gzip"
)
type resource string
2018-03-15 22:09:07 +01:00
// content types that are used in the ./assets,
// we could use the detectContentType that iris do but it's better
// to do it manually so we can test if that returns the correct result on embedding files.
func ( r resource ) contentType ( ) string {
switch filepath . Ext ( r . String ( ) ) {
case ".js" :
2020-04-27 11:28:30 +02:00
return "text/javascript"
2018-03-15 22:09:07 +01:00
case ".css" :
return "text/css"
case ".ico" :
return "image/x-icon"
case ".html" :
return "text/html"
default :
return "text/plain"
}
}
2018-03-14 06:17:35 +01:00
func ( r resource ) String ( ) string {
return string ( r )
}
func ( r resource ) strip ( strip string ) string {
s := r . String ( )
return strings . TrimPrefix ( s , strip )
}
func ( r resource ) loadFromBase ( dir string ) string {
filename := r . String ( )
filename = r . strip ( "/static" )
fullpath := filepath . Join ( dir , filename )
b , err := ioutil . ReadFile ( fullpath )
if err != nil {
panic ( fullpath + " failed with error: " + err . Error ( ) )
}
result := string ( b )
if runtime . GOOS != "windows" {
2021-01-09 04:41:20 +01:00
result = strings . ReplaceAll ( result , "\n" , "\r\n" )
2021-02-19 08:06:22 +01:00
result = strings . ReplaceAll ( result , "\r\r" , "" )
2018-03-14 06:17:35 +01:00
}
return result
}
var urls = [ ] resource {
2021-02-19 08:41:24 +01:00
"/static/css/main.css" ,
"/static/js/main.js" ,
2018-03-14 06:17:35 +01:00
"/static/favicon.ico" ,
}
// if bindata's values matches with the assets/... contents
2019-06-21 18:43:25 +02:00
// and secondly if the HandleDir had successfully registered
2018-03-14 06:17:35 +01:00
// the routes and gave the correct response.
func TestEmbeddingGzipFilesIntoApp ( t * testing . T ) {
2020-07-24 12:03:49 +02:00
dirOptions . Cache . Verbose = 0
2018-03-14 06:17:35 +01:00
app := newApp ( )
2020-07-24 12:03:49 +02:00
2018-03-14 06:17:35 +01:00
e := httptest . New ( t , app )
if runtime . GOOS != "windows" {
// remove the embedded static favicon for !windows,
// it should be built for unix-specific in order to be work
urls = urls [ 0 : len ( urls ) - 1 ]
}
for i , u := range urls {
url := u . String ( )
2020-07-24 12:03:49 +02:00
rawContents := u . loadFromBase ( "../embedding-files-into-app/assets" )
shouldBeCompressed := int64 ( len ( rawContents ) ) >= dirOptions . Cache . CompressMinSize
2018-03-14 06:17:35 +01:00
2020-07-24 12:03:49 +02:00
request := e . GET ( url )
if shouldBeCompressed {
request . WithHeader ( "Accept-Encoding" , "gzip" )
}
response := request . Expect ( )
2018-03-15 22:09:07 +01:00
response . ContentType ( u . contentType ( ) , app . ConfigurationReadOnly ( ) . GetCharset ( ) )
2020-07-24 12:03:49 +02:00
if shouldBeCompressed {
response . ContentEncoding ( "gzip" )
}
2018-03-14 06:17:35 +01:00
if expected , got := response . Raw ( ) . StatusCode , httptest . StatusOK ; expected != got {
t . Fatalf ( "[%d] of '%s': expected %d status code but got %d" , i , url , expected , got )
}
2019-07-15 06:49:04 +02:00
rawBody := response . Body ( ) . Raw ( )
2018-03-14 06:17:35 +01:00
2020-07-24 12:03:49 +02:00
if shouldBeCompressed {
2019-07-15 06:49:04 +02:00
reader , err := gzip . NewReader ( strings . NewReader ( rawBody ) )
2018-03-14 06:17:35 +01:00
defer reader . Close ( )
if err != nil {
t . Fatalf ( "[%d] of '%s': %v" , i , url , err )
}
buf := new ( bytes . Buffer )
reader . WriteTo ( buf )
2018-08-02 16:46:35 +02:00
if expected , got := rawContents , buf . String ( ) ; expected != got {
// t.Fatalf("[%d] of '%s': expected body:\n%s but got:\n%s", i, url, expected, got)
// let's reduce the output here...
// they are big files, no need to check for length here.
t . Fatalf ( "[%d] %s, expected body to look like: '%s...%s' but got '%s...%s'" , i , url , expected [ : 40 ] , expected [ len ( rawContents ) - 40 : ] , got [ : 40 ] , got [ len ( got ) - 40 : ] )
2018-03-14 06:17:35 +01:00
}
2020-07-24 12:03:49 +02:00
} else {
if expected , got := rawContents , rawBody ; expected != got {
t . Fatalf ( "[%d] %s, expected body to look like: '%s...%s' but got '%s...%s'" , i , url , expected [ : 40 ] , expected [ len ( rawContents ) - 40 : ] , got [ : 40 ] , got [ len ( got ) - 40 : ] )
}
}
2018-03-14 06:17:35 +01:00
}
}