测试Go(Golang)API传出请求而不实际触及第三方API

前端之家收集整理的这篇文章主要介绍了测试Go(Golang)API传出请求而不实际触及第三方API前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个Go API,它在内部后端和几个第三方API之间进行转换.我试图了解如何在不实际使用外部API的情况下测试其功能.

例如,这是一个处理传入请求以制作新歌曲的服务器,并将请求发送给第三方API:

package main

import (
        "bytes"
        "encoding/json"
        "fmt"
        "net/http"
)

var ThirdPartyApi = "http://www.coolsongssite.api"

type IncomingRequest struct {
        username string         `json:"username"`
        password string         `json:"password"`
        songs    []IncomingSong `json:"songs"`
}

type OutgoingRequest struct {
        username string         `json:"username"`
        password string         `json:"password"`
        songs    []OutgoingSong `json:"songs"`
}

type IncomingSong struct {
        artist string `json:"artist"`
        album  string `json:"album"`
        title  string `json:"title"`
}

type OutgoingSong struct {
        musician string `json:"musician"`
        record   string `json:"record"`
        name     string `json:"name"`
}

func main() {
        http.HandleFunc("/songs/create",createSong)
        http.ListenAndServe(":8080",nil)
}

func createSong(rw http.ResponseWriter,req *http.Request) {
         decoder := json.NewDecoder(req.Body)
         var incomingRequest IncomingRequest
         decoder.Decode(&incomingRequest)
         outgoingRequest := incomingRequestToOutgoingRequest(incomingRequest)

         r,_ := json.Marshal(outgoingRequest)
         request,_ := http.NewRequest("POST",ThirdPartyApi,bytes.NewBuffer(r))
         request.Header.Set("Content-Type","application/json")

         client := http.Client{}

         response,_ := client.Do(request)
         fmt.Fprintln(rw,response)
}

func incomingRequestToOutgoingRequest(inc IncomingRequest) OutgoingRequest {
        outgoingRequest := OutgoingRequest{
                username: inc.username,password: inc.password,}

        for _,s := range inc.songs {
                outgoingRequest.songs = append(
                        outgoingRequest.songs,OutgoingSong{
                                musician: s.artist,record:   s.album,name:     s.title,},)
        }

        return outgoingRequest
}

所以我可能会点击在localhost:8080上运行的应用程序:

curl -X POST http://localhost:8080/songs/new --data \
'{"username": "<my-username>","password": "<my-password>","songs": \
["artist": "<song-artist>","title": "<song-title>","album": "<song-album>"]}'

我的问题是:
如何编写测试来测试发出的请求(在本例中为http://www.coolsongssite.api)是否正确,而不实际发送?

我应该重写createSong处理程序,以便我可以隔离client.Do(请求)中发生的事情吗?

任何有关正确方向的帮助/建议/要点都会受到赞赏.

在这里我可以像这样测试incomingRequestToOutgoingRequest:

package main

import (
        "testing"
)

func TestincomingRequestToOutgoingRequest(t *testing.T) {
        incomingRequest := IncomingRequest{
                username: "myuser",password: "mypassword",}

        var songs []IncomingSong
        songs = append(
                songs,IncomingSong{
                artist: "White Rabbits",album:  "Milk Famous",title:  "I'm Not Me",)

        outgoingRequest := incomingRequestToOutgoingRequest(incomingRequest)

        if outgoingRequest.songs[0].musician != "White Rabbits" {
                t.Error("Expected musican name to be 'White Rabbits'. Got: ",outgoingRequest.songs[0].musician)
        }
}
您可以像这样使用net / http / httptest.NewServer:
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter,r *http.Request) {
    w.Write([]byte(`desired response here`))
}))
defer ts.Close()
ThirdPartyAPI = ts.URL
...
your test here

猜你在找的Go相关文章