Mailinglist Archive: yast-devel (77 mails)

< Previous Next >
[yast-devel] Webyast: HttpMock and utility for it
  • From: Josef Reidinger <jreidinger@xxxxxxx>
  • Date: Tue, 22 Dec 2009 11:22:20 +0100
  • Message-id: <200912221122.20554.jreidinger@xxxxxxx>
in short:
in branch for yast model I add also injector to HttpMock (standard library
which is part of ActiveResource to test Rest-service based models) so if you
plan write test look at it (or wait until it is merged).

in long:
Main goal to add models to webclient instead of dynamic one is easier testing
of functionality which should go to model as controller is only thin layer
which serves data. But I found that it is not so easy due to our design and
also not so simple situation with rest-service site data.
From my point of view correct testing of rest-service is provide same data as
expected from rest-service and test if it work as expected. Now if test is ever
present (not much models, I already fill bug) then it is huge mock which in
real doesn't test much functionality. So we need some mock service which mock
only target rest-service.
Why I choose HttpMock? Even if our local consultant suggest use another
framework I choose http because it looks like sufficient for our goal and it is
already in ActiveResource so we don't need another gems.
Why not use HttpMock without utilities? Of course it is possible, but you must
simulate resource which translate interface to path and also permissions
service which is out of test focus and test should not contain permissions
service details. Also there is problem with authentication which must be solved
on your own. So I create injector which add useful methods to HttpMock.

How to use it in test:
1) define response (we separate at least visually tested data and tests):


2) now mock network service ( I suggest use it in setup - but of course for
testing exceptions you should redefine it in method)
def setup
ActiveResource::HttpMock.set_authentification #a)
ActiveResource::HttpMock.respond_to do |mock|
header = ActiveResource::HttpMock.authentification_header #b)
mock.resources :'org.opensuse.yast.modules.test' => "/test",
:'org.opensuse.yast.modules.test2' => "/test2" #c)
mock.permissions "org.opensuse.yast.modules.test", { :synchronize => true }
mock.get "/test.xml", header, TEST_RESPONSE, 200 "/test.xml", header, TEST_RESPONSE, 200
mock.get "/test2.xml", header, TEST2_RESPONSE, 200 "/test2.xml", header, TEST2_RESPONSE, 200

a) this utility sets authentification and target machine to predefined values
b) authentification_header get hash which is passed to authenticate. It is
required in mock header. otherwise it is not matched, but it is hash, so you
can add your own values.
c) define resources ( often it is just one, but for example if you use two
rest-service (as time use time and ntp) then you define both. after hash
argument you pass hash with options, which contains key singleton and policy (
mentioned in previous mail why I found it useless so it is optional, without it
I use default which is no policy and single resource)
d) mock permissions controller, just pass prefix and then hash with permission
and if it is granted

rest of method is common HttpMock which return expected values, so you can
freely test it.

3) test model or controller :)
def test_find_and_save
test = TestModel.find :one
assert_equal "test",test.arg1 #see XML for arg1 value

Note: This utility class can be used without yast_model base model, because
also dynamic created ActiveResource use this resources (but I do not test it,
so expect problems)

If you don't have any complains or suggestion how to improve it I plan to merge
it together with yast_model.

welcome any questions, suggestions or complains

Josef Reidinger
YaST team
maintainer of perl-Bootloader, YaST2-Repair, webyast
To unsubscribe, e-mail: yast-devel+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-devel+help@xxxxxxxxxxxx

< Previous Next >
Follow Ups