-
-
Save aasumitro/4404506bed46a25529d722f1ff944267 to your computer and use it in GitHub Desktop.
func (handler *designerHandler) Notify(ctx *fiber.Ctx) error { | |
ctx.Set(fiber.HeaderContentType, "text/event-stream") | |
ctx.Set(fiber.HeaderCacheControl, "no-cache") | |
ctx.Set(fiber.HeaderConnection, "keep-alive") | |
ctx.Set(fiber.HeaderTransferEncoding, "chunked") | |
ctx.Context().SetBodyStreamWriter(func(w *bufio.Writer) { | |
for { | |
if handler.q.Length() == 0 { | |
time.Sleep(time.Second * 1) | |
continue | |
} | |
handler.mu.Lock() | |
item := handler.q.Remove() | |
if item == nil { | |
log.Printf("ERR_QUEUE: failed to get message\n") | |
handler.mu.Unlock() | |
return | |
} | |
if _, err := fmt.Fprintf(w, "data: %s\n\n", *item); err != nil { | |
log.Printf("ERR_WRITE: %v\n", err) | |
handler.mu.Unlock() | |
return | |
} | |
if err := w.Flush(); err != nil { | |
log.Printf("ERR_FLUSH: %v\n", err) | |
handler.mu.Unlock() | |
return | |
} | |
handler.mu.Unlock() | |
} | |
}) | |
return nil | |
} |
let sseSource = null; | |
const apiURL = "http://localhost:3000/designer"; | |
const sseURL = `${apiURL}/notifications`; | |
const confURL = `${apiURL}/configurations`; | |
document.addEventListener('DOMContentLoaded', () => { | |
if(typeof(EventSource) !== "undefined") { | |
sseSource = new EventSource(sseURL); | |
sseSource.addEventListener("message", function(event) { | |
const data = event.data; | |
showNotificationDialog(data); | |
}); | |
sseSource.addEventListener('error', function() { | |
if (sseSource.readyState === EventSource.CLOSED) { | |
sseSource = new EventSource(sseURL); | |
} | |
}); | |
} else { | |
alert("Sorry, your browser does not support server-sent events..."); | |
} | |
document.getElementById('copyright').textContent = ` | |
COPYRIGHT © ${new Date().getFullYear()} SKILLEDIN PTE. LTD. ALL RIGHTS RESERVED. | |
`; | |
}); |
aasumitro
commented
Jul 27, 2023
package reference_test
import (
"errors"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"skilledin.io/lms/internal/domain/entity"
"skilledin.io/lms/internal/module/reference"
"skilledin.io/lms/mocks"
"skilledin.io/lms/pkg/http/wrapper"
)
func Test_REST_Handler(_ *testing.T) {
app := fiber.New()
reference.NewRESTHandler(app, new(mocks.IReferenceService))
}
func Test_FetchCountry_ShouldSuccess(t *testing.T) {
app := fiber.New()
svcMock := new(mocks.IReferenceService)
svcMock.On("CountryList",
mock.Anything, mock.Anything, mock.Anything, mock.Anything,
).Return(&wrapper.PaginationResponse[entity.Reference]{}, nil).Once()
path := "/api/v1/references/countries"
handler := reference.RESTHandler{Service: svcMock}
app.Get(path, func(ctx *fiber.Ctx) error {
return ctx.Next()
}, handler.FetchCountry)
req := httptest.NewRequest("GET", path, nil)
res, err := app.Test(req)
assert.Nil(t, err)
assert.NotNil(t, res)
assert.Equal(t, res.StatusCode, fiber.StatusOK)
}
func Test_FetchCountry_ShouldError(t *testing.T) {
app := fiber.New()
svcMock := new(mocks.IReferenceService)
svcMock.On("CountryList",
mock.Anything, mock.Anything, mock.Anything, mock.Anything,
).Return(nil, errors.New("lorem")).Once()
path := "/api/v1/references/countries"
handler := reference.RESTHandler{Service: svcMock}
app.Get(path, func(ctx *fiber.Ctx) error {
return ctx.Next()
}, handler.FetchCountry)
req := httptest.NewRequest("GET", path, nil)
res, err := app.Test(req)
assert.Nil(t, err)
assert.NotNil(t, res)
assert.Equal(t, res.StatusCode, fiber.StatusBadRequest)
}
func Test_FetchCriticalSkill_ShouldSuccess(t *testing.T) {
app := fiber.New()
svcMock := new(mocks.IReferenceService)
svcMock.On("CriticalSkillList",
mock.Anything, mock.Anything, mock.Anything, mock.Anything,
).Return(&wrapper.PaginationResponse[entity.Reference]{}, nil).Once()
path := "/api/v1/references/critical-skills"
handler := reference.RESTHandler{Service: svcMock}
app.Get(path, func(ctx *fiber.Ctx) error {
return ctx.Next()
}, handler.FetchCriticalSkill)
req := httptest.NewRequest("GET", path, nil)
res, err := app.Test(req)
assert.Nil(t, err)
assert.NotNil(t, res)
assert.Equal(t, res.StatusCode, fiber.StatusOK)
}
func Test_FetchCriticalSkill_ShouldError(t *testing.T) {
app := fiber.New()
svcMock := new(mocks.IReferenceService)
svcMock.On("CriticalSkillList",
mock.Anything, mock.Anything, mock.Anything, mock.Anything,
).Return(nil, errors.New("lorem")).Once()
path := "/api/v1/references/critical-skills"
handler := reference.RESTHandler{Service: svcMock}
app.Get(path, func(ctx *fiber.Ctx) error {
return ctx.Next()
}, handler.FetchCriticalSkill)
req := httptest.NewRequest("GET", path, nil)
res, err := app.Test(req)
assert.Nil(t, err)
assert.NotNil(t, res)
assert.Equal(t, res.StatusCode, fiber.StatusBadRequest)
}
data, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{
Scopes: []string{"https://graph.microsoft.com/.default"},
})
fmt.Println(data.Token, err)