{"id":19893593,"url":"https://github.com/rzajac/golden","last_synced_at":"2025-05-02T19:31:07.798Z","repository":{"id":57550400,"uuid":"308355726","full_name":"rzajac/golden","owner":"rzajac","description":"Tests if HTTP request matches golden file.","archived":false,"fork":false,"pushed_at":"2022-01-21T10:09:29.000Z","size":128,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-20T17:05:55.069Z","etag":null,"topics":["golang","golden","golden-files","testing"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rzajac.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-10-29T14:36:29.000Z","updated_at":"2021-11-18T14:50:31.000Z","dependencies_parsed_at":"2022-08-29T22:30:47.984Z","dependency_job_id":null,"html_url":"https://github.com/rzajac/golden","commit_stats":null,"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rzajac%2Fgolden","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rzajac%2Fgolden/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rzajac%2Fgolden/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rzajac%2Fgolden/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rzajac","download_url":"https://codeload.github.com/rzajac/golden/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224328412,"owners_count":17293250,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","golden","golden-files","testing"],"created_at":"2024-11-12T18:30:02.228Z","updated_at":"2024-11-12T18:30:02.792Z","avatar_url":"https://github.com/rzajac.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"Golden is a small library to help with testing using golden files. Its main\npurpose (but not only one)  is to provide an easy way to describe \nHTTP request / response as YAML files. \n\n[![Go Report Card](https://goreportcard.com/badge/github.com/rzajac/golden)](https://goreportcard.com/report/github.com/rzajac/golden)\n[![GoDoc](https://img.shields.io/badge/api-Godoc-blue.svg)](https://pkg.go.dev/github.com/rzajac/golden)\n\n# Installation \n\n```\ngo get github.com/rzajac/golden\n```\n\n# Usage\n\n## Asserting\n\nLets say we have a golden file looking like this:\n\n```yaml\nbodyType: json\nbody: |\n    { \"key1\": \"val1\" }\n```\n\nHow to use it in test.\n\n```go\nfunc Test_Assert(t *testing.T) {\n    // --- Given --- \n    gld := golden.File(golden.Open(t, \"testdata/file.yaml\", nil))\n    \n    // --- When ---\n    data := []byte(`{\n        \"key1\": \"val1\"\n    }`)\n    \n    // --- Then --- \n    gld.Assert(data)\n}\n```\n\nBecause the `bodyType` was set to `json` the `data` in the test doesn't \nhave to be formatted exactly the same way as it's in the golden file. The\nlibrary is smart enough to compare data represented as JSON not the strings.  \n\nIf you need exact match set `bodyType` to `text`.  \n\n## Unmarshalling\n\n```go\ntype Data struct {\n    Key1 string `json:\"key1\"`\n}\n\nfunc Test_Unmarshal(t *testing.T) {\n    // --- Given ---\n    gld := golden.File(golden.Open(t, \"../testdata/file.yaml\", nil))\n\n    // --- When ---\n    data := \u0026Data{}\n    gld.Unmarshal(data)\n\n    // --- Then ---\n    if data.Key1 != \"val1\" {\n        t.Errorf(\"expected `%s` got `%s`\", \"val1\", data.Key1)\n    }\n}\n```\n\nIn this case golden file body will be unmarshalled (using `json.Unmarshal`)\nto structure `Data`. Any errors during unmarshalling will be handled by \n`Unmarshal` method.\n\n## Testing HTTP request / response\n\nGolden file describing the HTTP request and response:\n\n```yaml\nrequest:\n    method: POST\n    path: /some/path\n    query: key0=val0\u0026key1=val1\n    headers:\n        - 'Authorization: Bearer token'\n        - 'Content-Type: application/json'\n    bodyType: json\n    body: |\n        {\n          \"key2\": \"val2\"\n        }\n\nresponse:\n    statusCode: 200\n    headers:\n        - 'Content-Type: application/json'\n    bodyType: json\n    body: |\n        { \"success\": true }\n```\n\nExample test using golden file:\n\n```go\nfunc Test_Endpoint(t *testing.T) {\n    // --- Given ---\n    pth := \"testdata/request.yaml\"\n    gld := golden.Exchange(golden.Open(t, pth, nil))\n\n    // Setup mocks.\n    srvH, mckS := SrvMock()\n    mckS.On(\"CheckUserAccess\", \"token\").Return(true, nil)\n\n    // Prepare request recorder.\n    rec := httptest.NewRecorder()\n\n    // --- When ---\n    srvH.ServeHTTP(rec, gld.Request.Request())\n\n    // --- Then ---\n    mckS.AssertExpectations(t)\n    gld.Response.Assert(rec.Result())\n}\n```\n\n## Golden files as templates\n\nGolden files can also be used as Go templates when more dynamic approach \nis needed.\n\n```yaml\nrequest:\n    method: POST\n    path: /some/path\n    query: key0=val0\u0026key1=val1\n    headers:\n        - 'Authorization: Bearer {{ .token }}'\n        - 'Content-Type: application/json'\n    bodyType: json\n    body: |\n        {\n          \"key2\": \"val2\"\n        }\n\nresponse:\n    statusCode: 200\n    headers:\n        - 'Content-Type: application/json'\n    bodyType: json\n    body: |\n        { \"success\": true }\n```\n\n```go\nfunc Test_Endpoint(t *testing.T) {\n    // --- Given ---\n    token := GetTestToken()\n    tplD := make(golden.Map).Add(\"token\", token)\n    tpl := \"testdata/request.yaml\"\n    gld := golden.Exchange(golden.Open(t, tpl, tplD))\n\n    // Setup mocks.\n    srvH, mckS := SrvMock()\n    mckS.On(\"CheckUserAccess\", token).Return(true, nil)\n\n    // Prepare request recorder.\n    rec := httptest.NewRecorder()\n\n    // --- When ---\n    srvH.ServeHTTP(rec, gld.Request.Request())\n\n    // --- Then ---\n    mckS.AssertExpectations(t)\n    gld.Response.Assert(rec.Result())\n}\n```\n\nCheck out the documentation to see full API.\n\n## License\n\nApache License, Version 2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frzajac%2Fgolden","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frzajac%2Fgolden","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frzajac%2Fgolden/lists"}