github.com/edermi/gophish_mods@v0.7.0/controllers/static_test.go (about) 1 package controllers 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "os" 9 "path/filepath" 10 ) 11 12 var fileContent = []byte("Hello world") 13 14 func mustRemoveAll(dir string) { 15 err := os.RemoveAll(dir) 16 if err != nil { 17 panic(err) 18 } 19 } 20 21 func createTestFile(dir, filename string) error { 22 return ioutil.WriteFile(filepath.Join(dir, filename), fileContent, 0644) 23 } 24 25 func (s *ControllersSuite) TestGetStaticFile() { 26 dir, err := ioutil.TempDir("static/endpoint", "test-") 27 tempFolder := filepath.Base(dir) 28 29 s.Nil(err) 30 defer mustRemoveAll(dir) 31 32 err = createTestFile(dir, "foo.txt") 33 s.Nil(nil, err) 34 35 resp, err := http.Get(fmt.Sprintf("%s/static/%s/foo.txt", ps.URL, tempFolder)) 36 s.Nil(err) 37 38 defer resp.Body.Close() 39 got, err := ioutil.ReadAll(resp.Body) 40 s.Nil(err) 41 42 s.Equal(bytes.Compare(fileContent, got), 0, fmt.Sprintf("Got %s", got)) 43 } 44 45 func (s *ControllersSuite) TestStaticFileListing() { 46 dir, err := ioutil.TempDir("static/endpoint", "test-") 47 tempFolder := filepath.Base(dir) 48 49 s.Nil(err) 50 defer mustRemoveAll(dir) 51 52 err = createTestFile(dir, "foo.txt") 53 s.Nil(nil, err) 54 55 resp, err := http.Get(fmt.Sprintf("%s/static/%s/", ps.URL, tempFolder)) 56 s.Nil(err) 57 58 defer resp.Body.Close() 59 s.Nil(err) 60 s.Equal(resp.StatusCode, http.StatusNotFound) 61 } 62 63 func (s *ControllersSuite) TestStaticIndex() { 64 dir, err := ioutil.TempDir("static/endpoint", "test-") 65 tempFolder := filepath.Base(dir) 66 67 s.Nil(err) 68 defer mustRemoveAll(dir) 69 70 err = createTestFile(dir, "index.html") 71 s.Nil(nil, err) 72 73 resp, err := http.Get(fmt.Sprintf("%s/static/%s/", ps.URL, tempFolder)) 74 s.Nil(err) 75 76 defer resp.Body.Close() 77 got, err := ioutil.ReadAll(resp.Body) 78 s.Nil(err) 79 80 s.Equal(bytes.Compare(fileContent, got), 0, fmt.Sprintf("Got %s", got)) 81 }