github.com/speelynet/server@v0.2.10/stepdefs_test.go (about)

     1  package main_test
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/cucumber/godog"
     6  	. "github.com/speelynet/server"
     7  	"io/ioutil"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"os"
    11  	"path"
    12  	"strings"
    13  )
    14  
    15  var (
    16  	ms          *httptest.Server
    17  	currentFile *os.File
    18  	fileContent string
    19  	response    string
    20  )
    21  
    22  func iCreateAMockServer() error {
    23  	ms = httptest.NewServer(CreateRouter())
    24  	return nil
    25  }
    26  
    27  func iCreateTheTemporaryFile(filepath string) error {
    28  	dir := path.Dir(filepath)
    29  
    30  	if e := os.MkdirAll(dir, os.ModePerm); e != nil {
    31  		return e
    32  	} else if currentFile, e = os.Create(filepath); e != nil {
    33  		return e
    34  	}
    35  
    36  	return nil
    37  }
    38  
    39  func iGoToMockServer(url string) error {
    40  	if r, e := http.Get(ms.URL + url); e != nil {
    41  		return e
    42  	} else if b, e := ioutil.ReadAll(r.Body); e != nil {
    43  		return e
    44  	} else {
    45  		response = string(b)
    46  	}
    47  	return nil
    48  }
    49  
    50  func iShouldSeeTheFileContent() error {
    51  	indent := func(s string) string {
    52  		return "    " + strings.ReplaceAll(s, "\n", "\n    ")
    53  	}
    54  
    55  	if response != fileContent {
    56  		return fmt.Errorf(indent(fmt.Sprintf("\nexpected:\n%s\ngot:\n%s\n", indent(fileContent), indent(response))))
    57  	}
    58  	return nil
    59  }
    60  
    61  func theContentOfTheTemporaryFileIs(content *godog.DocString) error {
    62  	if _, e := currentFile.WriteString(content.Content); e != nil {
    63  		return e
    64  	}
    65  	fileContent = content.Content
    66  
    67  	return nil
    68  }
    69  
    70  func InitializeTestSuite(ctx *godog.TestSuiteContext) {
    71  	ctx.BeforeSuite(func() {
    72  		_ = os.Mkdir("testdata", os.ModePerm)
    73  		_ = os.Chdir("testdata")
    74  	})
    75  	ctx.AfterSuite(func() {
    76  		_ = os.Chdir("..")
    77  		_ = os.RemoveAll("testdata")
    78  	})
    79  }
    80  
    81  func InitializeScenario(ctx *godog.ScenarioContext) {
    82  	ctx.Step(`^I create a mock server$`, iCreateAMockServer)
    83  	ctx.Step(`^I create the temporary file "([^"]*)"$`, iCreateTheTemporaryFile)
    84  	ctx.Step(`^I go to "mock.server([^"]*)"$`, iGoToMockServer)
    85  	ctx.Step(`^I should see the file content$`, iShouldSeeTheFileContent)
    86  	ctx.Step(`^the content of the temporary file is$`, theContentOfTheTemporaryFileIs)
    87  
    88  	ctx.AfterScenario(func(s *godog.Scenario, e error) {
    89  		if ms != nil {
    90  			ms.Close()
    91  		}
    92  		ms = nil
    93  
    94  		if currentFile != nil {
    95  			_ = currentFile.Close()
    96  			_ = os.Remove(currentFile.Name())
    97  		}
    98  		currentFile = nil
    99  
   100  		fileContent = ""
   101  
   102  		_ = os.RemoveAll("*")
   103  	})
   104  }