github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/globalsearch/globalsearchv2/search_test.go (about)

     1  package globalsearchv2
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  
     7  	bluemix "github.com/IBM-Cloud/bluemix-go"
     8  	"github.com/IBM-Cloud/bluemix-go/client"
     9  	bluemixHttp "github.com/IBM-Cloud/bluemix-go/http"
    10  	"github.com/IBM-Cloud/bluemix-go/session"
    11  
    12  	"github.com/onsi/gomega/ghttp"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("Tasks", func() {
    19  	var server *ghttp.Server
    20  	AfterEach(func() {
    21  		server.Close()
    22  	})
    23  	Describe("PostQuery", func() {
    24  		Context("When PostQuery is successful", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodPost, "/v2/resources/search"),
    30  						ghttp.RespondWith(http.StatusOK, `
    31                             {
    32                              "items": {
    33                              }
    34                            }
    35                          `),
    36  					),
    37  				)
    38  			})
    39  
    40  			It("should return query results", func() {
    41  				token := ""
    42  				fields := []string{""}
    43  				query := ""
    44  				searchBody := SearchBody{
    45  					Query:  query,
    46  					Fields: fields,
    47  					Token:  token,
    48  				}
    49  				searchResult, err := newSearch(server.URL()).PostQuery(searchBody)
    50  				Expect(err).NotTo(HaveOccurred())
    51  				Expect(searchResult).ShouldNot(BeNil())
    52  				//Expect(searchResult.Items).Should(Equal("5abb6a7d11a1a5001479a0ac"))
    53  
    54  			})
    55  		})
    56  		Context("When PostQuery is unsuccessful", func() {
    57  			BeforeEach(func() {
    58  				server = ghttp.NewServer()
    59  				server.SetAllowUnhandledRequests(true)
    60  				server.AppendHandlers(
    61  					ghttp.CombineHandlers(
    62  						ghttp.VerifyRequest(http.MethodPost, "/v2/resources/search"),
    63  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to get query results`),
    64  					),
    65  				)
    66  			})
    67  
    68  			It("should return error during post query", func() {
    69  				token := ""
    70  				fields := []string{""}
    71  				query := ""
    72  				searchBody := SearchBody{
    73  					Query:  query,
    74  					Fields: fields,
    75  					Token:  token,
    76  				}
    77  				searchResult, err := newSearch(server.URL()).PostQuery(searchBody)
    78  				Expect(err).To(HaveOccurred())
    79  				Expect(searchResult.Items).Should(Equal(""))
    80  			})
    81  		})
    82  	})
    83  })
    84  
    85  func newSearch(url string) Searches {
    86  
    87  	sess, err := session.New()
    88  	if err != nil {
    89  		log.Fatal(err)
    90  	}
    91  	conf := sess.Config.Copy()
    92  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
    93  	conf.Endpoint = &url
    94  
    95  	client := client.Client{
    96  		Config:      conf,
    97  		ServiceName: bluemix.GlobalSearchService,
    98  	}
    99  	return newSearchAPI(&client)
   100  }