github.com/nuvolaris/nuv@v0.0.0-20240511174247-a74e3a52bfd8/serve_test.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one
     2  // or more contributor license agreements.  See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership.  The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License.  You may obtain a copy of the License at
     8  //
     9  //   http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package main
    19  
    20  import (
    21  	"io"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"os"
    25  	"path/filepath"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestIndex(t *testing.T) {
    32  	_ = os.Chdir(workDir)
    33  	webDir, _ := filepath.Abs(filepath.Join("tests", "olaris", "web"))
    34  	handler := webFileServerHandler(webDir)
    35  	ts := httptest.NewServer(handler)
    36  	defer ts.Close()
    37  
    38  	newreq := func(method, url string, body io.Reader) *http.Request {
    39  		r, err := http.NewRequest(method, url, body)
    40  		if err != nil {
    41  			t.Fatal(err)
    42  		}
    43  		return r
    44  	}
    45  
    46  	tests := []struct {
    47  		name string
    48  		r    *http.Request
    49  	}{
    50  		{name: "1: testing get", r: newreq("GET", ts.URL+"/", nil)},
    51  		// {name: "2: testing post", r: newreq("POST", ts.URL+"/", nil)},
    52  	}
    53  
    54  	for _, tt := range tests {
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			resp, err := http.DefaultClient.Do(tt.r)
    57  			require.NoError(t, err)
    58  			require.NotNil(t, resp)
    59  			require.Equal(t, http.StatusOK, resp.StatusCode)
    60  			resp.Body.Close()
    61  		})
    62  	}
    63  }