github.com/wfusion/gofusion@v1.1.14/test/http/cases/file_test.go (about)

     1  package cases
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/gin-gonic/gin"
    15  	"github.com/stretchr/testify/suite"
    16  
    17  	"github.com/wfusion/gofusion/common/env"
    18  	"github.com/wfusion/gofusion/log"
    19  
    20  	fusHtp "github.com/wfusion/gofusion/http"
    21  	testHtp "github.com/wfusion/gofusion/test/http"
    22  )
    23  
    24  func TestFile(t *testing.T) {
    25  	testingSuite := &File{Test: new(testHtp.Test)}
    26  	testingSuite.Init(testingSuite)
    27  	suite.Run(t, testingSuite)
    28  }
    29  
    30  type File struct {
    31  	*testHtp.Test
    32  }
    33  
    34  func (t *File) BeforeTest(suiteName, testName string) {
    35  	t.Catch(func() {
    36  		log.Info(context.Background(), "right before %s %s", suiteName, testName)
    37  	})
    38  }
    39  
    40  func (t *File) AfterTest(suiteName, testName string) {
    41  	t.Catch(func() {
    42  		log.Info(context.Background(), "right after %s %s", suiteName, testName)
    43  	})
    44  }
    45  
    46  func (t *File) TestStatic() {
    47  	t.Catch(func() {
    48  		// Given
    49  		files := t.ConfigFiles()
    50  		p := filepath.Join(env.WorkDir, fmt.Sprintf("/configs/%s.%s", t.AppName(), files[len(files)-1]))
    51  		w := httptest.NewRecorder()
    52  		req, err := http.NewRequest(http.MethodGet, "/TestStatic.yml", nil)
    53  		t.NoError(err)
    54  		engine := t.ServerGiven("File", "/TestStatic.yml", p)
    55  
    56  		// When
    57  		engine.ServeHTTP(w, req)
    58  
    59  		// Then
    60  		t.Equal(http.StatusOK, w.Code)
    61  		t.NotEmpty(w.Body.String())
    62  	})
    63  }
    64  
    65  func (t *File) TestStaticZeroCopy() {
    66  	t.Catch(func() {
    67  		// Given
    68  		files := t.ConfigFiles()
    69  		p := filepath.Join(env.WorkDir, fmt.Sprintf("/configs/%s.%s", t.AppName(), files[len(files)-1]))
    70  		w := httptest.NewRecorder()
    71  		req, err := http.NewRequest(http.MethodGet, "/TestStaticZeroCopy.yml", nil)
    72  		t.NoError(err)
    73  		engine := t.ServerGiven("File", "/TestStaticZeroCopyMock.yml", p)
    74  		engine.GET("/TestStaticZeroCopy.yml", fusHtp.StaticFileZeroCopy(p))
    75  
    76  		// When
    77  		engine.ServeHTTP(w, req)
    78  
    79  		// Then
    80  		t.Equal(http.StatusOK, w.Code)
    81  		t.NotEmpty(w.Body.String())
    82  	})
    83  }
    84  
    85  func (t *File) TestContentZeroCopy() {
    86  	t.Catch(func() {
    87  		// Given
    88  		files := t.ConfigFiles()
    89  		p := filepath.Join(env.WorkDir, fmt.Sprintf("/configs/%s.%s", t.AppName(), files[len(files)-1]))
    90  		w := httptest.NewRecorder()
    91  		req, err := http.NewRequest(http.MethodGet, "/TestContentZeroCopy.yml", nil)
    92  		t.NoError(err)
    93  		engine := t.ServerGiven("File", "/TestContentZeroCopyMock.yml", p)
    94  		engine.GET("/TestContentZeroCopy.yml", fusHtp.ContentZeroCopy(func(c *gin.Context) (
    95  			name string, modTime time.Time, content io.ReadSeeker, err error) {
    96  			f, err := os.Open(p)
    97  			if err != nil {
    98  				return
    99  			}
   100  			s, err := f.Stat()
   101  			if err != nil {
   102  				return
   103  			}
   104  			return f.Name(), s.ModTime(), f, nil
   105  		}))
   106  
   107  		// When
   108  		engine.ServeHTTP(w, req)
   109  
   110  		// Then
   111  		t.Equal(http.StatusOK, w.Code)
   112  		t.NotEmpty(w.Body.String())
   113  	})
   114  }